blob: f2ada5f0d8438dbd88f51be49ea4f0634cc97761 [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
Victor Stinnerbae2d622015-10-01 09:47:30 +020012# ifdef HAVE_GETRANDOM
13# include <sys/random.h>
14# elif defined(HAVE_GETRANDOM_SYSCALL)
Victor Stinner59f7fb22015-03-18 14:39:33 +010015# include <sys/syscall.h>
Victor Stinner59f7fb22015-03-18 14:39:33 +010016# endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010017#endif
18
Benjamin Peterson69e97272012-02-21 11:08:50 -050019#ifdef Py_DEBUG
20int _Py_HashSecret_Initialized = 0;
21#else
22static int _Py_HashSecret_Initialized = 0;
23#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010024
25#ifdef MS_WINDOWS
Georg Brandl2daf6ae2012-02-20 19:54:16 +010026static HCRYPTPROV hCryptProv = 0;
27
28static int
29win32_urandom_init(int raise)
30{
Georg Brandl2daf6ae2012-02-20 19:54:16 +010031 /* Acquire context */
Martin v. Löwis3f50bf62013-01-25 14:06:18 +010032 if (!CryptAcquireContext(&hCryptProv, NULL, NULL,
33 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010034 goto error;
35
36 return 0;
37
38error:
39 if (raise)
40 PyErr_SetFromWindowsErr(0);
41 else
42 Py_FatalError("Failed to initialize Windows random API (CryptoGen)");
43 return -1;
44}
45
46/* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen
Victor Stinner4d6a3d62014-12-21 01:16:38 +010047 API. Return 0 on success, or raise an exception and return -1 on error. */
Georg Brandl2daf6ae2012-02-20 19:54:16 +010048static int
49win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
50{
51 Py_ssize_t chunk;
52
53 if (hCryptProv == 0)
54 {
55 if (win32_urandom_init(raise) == -1)
56 return -1;
57 }
58
59 while (size > 0)
60 {
61 chunk = size > INT_MAX ? INT_MAX : size;
Victor Stinner0c083462013-11-15 23:26:25 +010062 if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010063 {
64 /* CryptGenRandom() failed */
65 if (raise)
66 PyErr_SetFromWindowsErr(0);
67 else
68 Py_FatalError("Failed to initialized the randomized hash "
69 "secret using CryptoGen)");
70 return -1;
71 }
72 buffer += chunk;
73 size -= chunk;
74 }
75 return 0;
76}
Georg Brandl2daf6ae2012-02-20 19:54:16 +010077
Martin Panter39b10252016-06-10 08:07:11 +000078/* Issue #25003: Don't use getentropy() on Solaris (available since
79 * Solaris 11.3), it is blocking whereas os.urandom() should not block. */
Victor Stinnerbae2d622015-10-01 09:47:30 +020080#elif defined(HAVE_GETENTROPY) && !defined(sun)
81#define PY_GETENTROPY 1
82
Victor Stinner4d6a3d62014-12-21 01:16:38 +010083/* Fill buffer with size pseudo-random bytes generated by getentropy().
84 Return 0 on success, or raise an exception and return -1 on error.
Georg Brandl2daf6ae2012-02-20 19:54:16 +010085
Victor Stinner4d6a3d62014-12-21 01:16:38 +010086 If fatal is nonzero, call Py_FatalError() instead of raising an exception
87 on error. */
88static int
89py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
90{
91 while (size > 0) {
92 Py_ssize_t len = Py_MIN(size, 256);
Victor Stinner9aa13312015-03-30 11:18:30 +020093 int res;
94
95 if (!fatal) {
96 Py_BEGIN_ALLOW_THREADS
97 res = getentropy(buffer, len);
98 Py_END_ALLOW_THREADS
99
100 if (res < 0) {
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100101 PyErr_SetFromErrno(PyExc_OSError);
102 return -1;
103 }
104 }
Victor Stinner9aa13312015-03-30 11:18:30 +0200105 else {
106 res = getentropy(buffer, len);
107 if (res < 0)
108 Py_FatalError("getentropy() failed");
109 }
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 Stinneraf597322016-09-20 22:26:18 +0200122/* Call getrandom()
123 - Return 1 on success
Victor Stinner6d8bc462016-09-20 22:46:02 +0200124 - Return 0 if getrandom() syscall is not available (failed with ENOSYS or
125 EPERM) or if getrandom(GRND_NONBLOCK) failed with EAGAIN (system urandom
Victor Stinneraf597322016-09-20 22:26:18 +0200126 not initialized yet) and raise=0.
127 - 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
131py_getrandom(void *buffer, Py_ssize_t size, int raise)
132{
Victor Stinneraf597322016-09-20 22:26:18 +0200133 /* Is getrandom() supported by the running kernel? Set to 0 if getrandom()
Victor Stinner6d8bc462016-09-20 22:46:02 +0200134 failed with ENOSYS or EPERM. Need Linux kernel 3.17 or newer, or Solaris
Victor Stinneraf597322016-09-20 22:26:18 +0200135 11.3 or newer */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100136 static int getrandom_works = 1;
Victor Stinnerdddf4842016-06-07 11:21:42 +0200137
138 /* getrandom() on Linux will block if called before the kernel has
139 * initialized the urandom entropy pool. This will cause Python
140 * to hang on startup if called very early in the boot process -
141 * see https://bugs.python.org/issue26839. To avoid this, use the
142 * GRND_NONBLOCK flag. */
143 const int flags = GRND_NONBLOCK;
Victor Stinnerec721f32016-06-16 23:53:47 +0200144 long n;
Victor Stinner59f7fb22015-03-18 14:39:33 +0100145
Victor Stinneraf597322016-09-20 22:26:18 +0200146 if (!getrandom_works) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100147 return 0;
Victor Stinneraf597322016-09-20 22:26:18 +0200148 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100149
150 while (0 < size) {
Victor Stinner9d242712016-04-12 22:28:49 +0200151#ifdef sun
152 /* Issue #26735: On Solaris, getrandom() is limited to returning up
153 to 1024 bytes */
154 n = Py_MIN(size, 1024);
155#else
Victor Stinnerec721f32016-06-16 23:53:47 +0200156 n = Py_MIN(size, LONG_MAX);
Victor Stinner9d242712016-04-12 22:28:49 +0200157#endif
Victor Stinner79b74ae2015-03-30 11:16:40 +0200158
Victor Stinner9d242712016-04-12 22:28:49 +0200159 errno = 0;
Victor Stinnerbae2d622015-10-01 09:47:30 +0200160#ifdef HAVE_GETRANDOM
161 if (raise) {
162 Py_BEGIN_ALLOW_THREADS
Victor Stinner9d242712016-04-12 22:28:49 +0200163 n = getrandom(buffer, n, flags);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200164 Py_END_ALLOW_THREADS
165 }
166 else {
Victor Stinner9d242712016-04-12 22:28:49 +0200167 n = getrandom(buffer, n, flags);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200168 }
169#else
170 /* On Linux, use the syscall() function because the GNU libc doesn't
171 * expose the Linux getrandom() syscall yet. See:
Victor Stinner59f7fb22015-03-18 14:39:33 +0100172 * https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */
Victor Stinner79b74ae2015-03-30 11:16:40 +0200173 if (raise) {
174 Py_BEGIN_ALLOW_THREADS
Victor Stinner9d242712016-04-12 22:28:49 +0200175 n = syscall(SYS_getrandom, buffer, n, flags);
Victor Stinner79b74ae2015-03-30 11:16:40 +0200176 Py_END_ALLOW_THREADS
177 }
178 else {
Victor Stinner9d242712016-04-12 22:28:49 +0200179 n = syscall(SYS_getrandom, buffer, n, flags);
Victor Stinner79b74ae2015-03-30 11:16:40 +0200180 }
Victor Stinnerbae2d622015-10-01 09:47:30 +0200181#endif
Victor Stinner79b74ae2015-03-30 11:16:40 +0200182
Victor Stinner59f7fb22015-03-18 14:39:33 +0100183 if (n < 0) {
Victor Stinneraf597322016-09-20 22:26:18 +0200184 /* ENOSYS: getrandom() syscall not supported by the kernel (but
Victor Stinner6d8bc462016-09-20 22:46:02 +0200185 * maybe supported by the host which built Python). EPERM:
186 * getrandom() syscall blocked by SECCOMP or something else. */
187 if (errno == ENOSYS || errno == EPERM) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100188 getrandom_works = 0;
189 return 0;
190 }
Victor Stinnerdddf4842016-06-07 11:21:42 +0200191 if (errno == EAGAIN) {
Victor Stinneraf597322016-09-20 22:26:18 +0200192 /* getrandom(GRND_NONBLOCK) fails with EAGAIN if the system
193 urandom is not initialiazed yet. In this case, fall back on
194 reading from /dev/urandom.
195
196 Note: In this case the data read will not be random so
197 should not be used for cryptographic purposes. Retaining
198 the existing semantics for practical purposes. */
Victor Stinnerdddf4842016-06-07 11:21:42 +0200199 getrandom_works = 0;
200 return 0;
201 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100202
203 if (errno == EINTR) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100204 if (PyErr_CheckSignals()) {
Victor Stinneraf597322016-09-20 22:26:18 +0200205 if (!raise) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100206 Py_FatalError("getrandom() interrupted by a signal");
Victor Stinneraf597322016-09-20 22:26:18 +0200207 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100208 return -1;
209 }
Victor Stinneraf597322016-09-20 22:26:18 +0200210
Victor Stinner59f7fb22015-03-18 14:39:33 +0100211 /* retry getrandom() */
212 continue;
213 }
214
Victor Stinneraf597322016-09-20 22:26:18 +0200215 if (raise) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100216 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinneraf597322016-09-20 22:26:18 +0200217 }
218 else {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100219 Py_FatalError("getrandom() failed");
Victor Stinneraf597322016-09-20 22:26:18 +0200220 }
Victor Stinner59f7fb22015-03-18 14:39:33 +0100221 return -1;
222 }
223
224 buffer += n;
225 size -= n;
226 }
227 return 1;
228}
229#endif
230
Antoine Pitroue472aea2014-04-26 14:33:03 +0200231static struct {
232 int fd;
233 dev_t st_dev;
234 ino_t st_ino;
235} urandom_cache = { -1 };
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100236
Victor Stinner59f7fb22015-03-18 14:39:33 +0100237
Victor Stinneraf597322016-09-20 22:26:18 +0200238/* Read 'size' random bytes from py_getrandom(). Fall back on reading from
239 /dev/urandom if getrandom() is not available.
240
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100241 Call Py_FatalError() on error. */
242static void
Christian Heimes985ecdc2013-11-20 11:46:18 +0100243dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100244{
245 int fd;
246 Py_ssize_t n;
247
248 assert (0 < size);
249
Victor Stinnerbae2d622015-10-01 09:47:30 +0200250#ifdef PY_GETRANDOM
Victor Stinneraf597322016-09-20 22:26:18 +0200251 if (py_getrandom(buffer, size, 0) == 1) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100252 return;
Victor Stinneraf597322016-09-20 22:26:18 +0200253 }
Victor Stinner6d8bc462016-09-20 22:46:02 +0200254 /* getrandom() failed with ENOSYS or EPERM,
Victor Stinneraf597322016-09-20 22:26:18 +0200255 fall back on reading /dev/urandom */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100256#endif
257
Victor Stinnerc7cd12d2015-03-19 23:24:45 +0100258 fd = _Py_open_noraise("/dev/urandom", O_RDONLY);
Victor Stinneraf597322016-09-20 22:26:18 +0200259 if (fd < 0) {
Victor Stinnerc7cd12d2015-03-19 23:24:45 +0100260 Py_FatalError("Failed to open /dev/urandom");
Victor Stinneraf597322016-09-20 22:26:18 +0200261 }
Victor Stinnerc7cd12d2015-03-19 23:24:45 +0100262
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100263 while (0 < size)
264 {
265 do {
266 n = read(fd, buffer, (size_t)size);
267 } while (n < 0 && errno == EINTR);
Victor Stinneraf597322016-09-20 22:26:18 +0200268
269 if (n <= 0) {
270 /* read() failed or returned 0 bytes */
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100271 Py_FatalError("Failed to read bytes from /dev/urandom");
272 break;
273 }
274 buffer += n;
Victor Stinnerc72828b2016-06-14 16:35:49 +0200275 size -= n;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100276 }
277 close(fd);
278}
279
Victor Stinneraf597322016-09-20 22:26:18 +0200280/* Read 'size' random bytes from py_getrandom(). Fall back on reading from
281 /dev/urandom if getrandom() is not available.
282
283 Return 0 on success. Raise an exception and return -1 on error. */
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100284static int
285dev_urandom_python(char *buffer, Py_ssize_t size)
286{
287 int fd;
288 Py_ssize_t n;
Steve Dowerf2f373f2015-02-21 08:44:05 -0800289 struct _Py_stat_struct st;
Victor Stinnerbae2d622015-10-01 09:47:30 +0200290#ifdef PY_GETRANDOM
Victor Stinner59f7fb22015-03-18 14:39:33 +0100291 int res;
292#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100293
294 if (size <= 0)
295 return 0;
296
Victor Stinnerbae2d622015-10-01 09:47:30 +0200297#ifdef PY_GETRANDOM
Victor Stinner59f7fb22015-03-18 14:39:33 +0100298 res = py_getrandom(buffer, size, 1);
Victor Stinneraf597322016-09-20 22:26:18 +0200299 if (res < 0) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100300 return -1;
Victor Stinneraf597322016-09-20 22:26:18 +0200301 }
302 if (res == 1) {
Victor Stinner59f7fb22015-03-18 14:39:33 +0100303 return 0;
Victor Stinneraf597322016-09-20 22:26:18 +0200304 }
Victor Stinner6d8bc462016-09-20 22:46:02 +0200305 /* getrandom() failed with ENOSYS or EPERM,
Victor Stinneraf597322016-09-20 22:26:18 +0200306 fall back on reading /dev/urandom */
Victor Stinner59f7fb22015-03-18 14:39:33 +0100307#endif
308
Antoine Pitroue472aea2014-04-26 14:33:03 +0200309 if (urandom_cache.fd >= 0) {
310 /* Does the fd point to the same thing as before? (issue #21207) */
Victor Stinnere134a7f2015-03-30 10:09:31 +0200311 if (_Py_fstat_noraise(urandom_cache.fd, &st)
Antoine Pitroue472aea2014-04-26 14:33:03 +0200312 || st.st_dev != urandom_cache.st_dev
313 || st.st_ino != urandom_cache.st_ino) {
314 /* Something changed: forget the cached fd (but don't close it,
315 since it probably points to something important for some
316 third-party code). */
317 urandom_cache.fd = -1;
318 }
319 }
320 if (urandom_cache.fd >= 0)
321 fd = urandom_cache.fd;
Antoine Pitrou4879a962013-08-31 00:26:02 +0200322 else {
Antoine Pitrou4879a962013-08-31 00:26:02 +0200323 fd = _Py_open("/dev/urandom", O_RDONLY);
Victor Stinnera555cfc2015-03-18 00:22:14 +0100324 if (fd < 0) {
Antoine Pitrou4879a962013-08-31 00:26:02 +0200325 if (errno == ENOENT || errno == ENXIO ||
326 errno == ENODEV || errno == EACCES)
327 PyErr_SetString(PyExc_NotImplementedError,
328 "/dev/urandom (or equivalent) not found");
Victor Stinnera555cfc2015-03-18 00:22:14 +0100329 /* otherwise, keep the OSError exception raised by _Py_open() */
Antoine Pitrou4879a962013-08-31 00:26:02 +0200330 return -1;
331 }
Antoine Pitroue472aea2014-04-26 14:33:03 +0200332 if (urandom_cache.fd >= 0) {
Antoine Pitrou4879a962013-08-31 00:26:02 +0200333 /* urandom_fd was initialized by another thread while we were
334 not holding the GIL, keep it. */
335 close(fd);
Antoine Pitroue472aea2014-04-26 14:33:03 +0200336 fd = urandom_cache.fd;
Antoine Pitrou4879a962013-08-31 00:26:02 +0200337 }
Antoine Pitroue472aea2014-04-26 14:33:03 +0200338 else {
Steve Dowerf2f373f2015-02-21 08:44:05 -0800339 if (_Py_fstat(fd, &st)) {
Antoine Pitroue472aea2014-04-26 14:33:03 +0200340 close(fd);
341 return -1;
342 }
343 else {
344 urandom_cache.fd = fd;
345 urandom_cache.st_dev = st.st_dev;
346 urandom_cache.st_ino = st.st_ino;
347 }
348 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100349 }
350
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100351 do {
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100352 n = _Py_read(fd, buffer, (size_t)size);
Victor Stinneraf597322016-09-20 22:26:18 +0200353 if (n == -1) {
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100354 return -1;
Victor Stinneraf597322016-09-20 22:26:18 +0200355 }
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100356 if (n == 0) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100357 PyErr_Format(PyExc_RuntimeError,
Victor Stinnerc9382eb2015-03-19 23:36:33 +0100358 "Failed to read %zi bytes from /dev/urandom",
359 size);
360 return -1;
361 }
362
363 buffer += n;
364 size -= n;
365 } while (0 < size);
366
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100367 return 0;
368}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200369
370static void
371dev_urandom_close(void)
372{
Antoine Pitroue472aea2014-04-26 14:33:03 +0200373 if (urandom_cache.fd >= 0) {
374 close(urandom_cache.fd);
375 urandom_cache.fd = -1;
Antoine Pitrou4879a962013-08-31 00:26:02 +0200376 }
377}
378
Victor Stinnerbae2d622015-10-01 09:47:30 +0200379#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100380
381/* Fill buffer with pseudo-random bytes generated by a linear congruent
382 generator (LCG):
383
384 x(n+1) = (x(n) * 214013 + 2531011) % 2^32
385
386 Use bits 23..16 of x(n) to generate a byte. */
387static void
388lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size)
389{
390 size_t index;
391 unsigned int x;
392
393 x = x0;
394 for (index=0; index < size; index++) {
395 x *= 214013;
396 x += 2531011;
397 /* modulo 2 ^ (8 * sizeof(int)) */
398 buffer[index] = (x >> 16) & 0xff;
399 }
400}
401
Georg Brandlc6a2c9b2013-10-06 18:43:19 +0200402/* Fill buffer with size pseudo-random bytes from the operating system random
Serhiy Storchaka56a6d852014-12-01 18:28:43 +0200403 number generator (RNG). It is suitable for most cryptographic purposes
Georg Brandlc6a2c9b2013-10-06 18:43:19 +0200404 except long living private keys for asymmetric encryption.
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100405
406 Return 0 on success, raise an exception and return -1 on error. */
407int
408_PyOS_URandom(void *buffer, Py_ssize_t size)
409{
410 if (size < 0) {
411 PyErr_Format(PyExc_ValueError,
412 "negative argument not allowed");
413 return -1;
414 }
415 if (size == 0)
416 return 0;
417
418#ifdef MS_WINDOWS
419 return win32_urandom((unsigned char *)buffer, size, 1);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200420#elif defined(PY_GETENTROPY)
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100421 return py_getentropy(buffer, size, 0);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100422#else
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100423 return dev_urandom_python((char*)buffer, size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100424#endif
425}
426
427void
428_PyRandom_Init(void)
429{
430 char *env;
Christian Heimes985ecdc2013-11-20 11:46:18 +0100431 unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500432 Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
Christian Heimes985ecdc2013-11-20 11:46:18 +0100433 assert(secret_size == sizeof(_Py_HashSecret.uc));
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100434
Benjamin Peterson69e97272012-02-21 11:08:50 -0500435 if (_Py_HashSecret_Initialized)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100436 return;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500437 _Py_HashSecret_Initialized = 1;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100438
439 /*
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100440 Hash randomization is enabled. Generate a per-process secret,
441 using PYTHONHASHSEED if provided.
442 */
443
444 env = Py_GETENV("PYTHONHASHSEED");
Georg Brandl12897d72012-02-20 23:49:29 +0100445 if (env && *env != '\0' && strcmp(env, "random") != 0) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100446 char *endptr = env;
447 unsigned long seed;
448 seed = strtoul(env, &endptr, 10);
449 if (*endptr != '\0'
450 || seed > 4294967295UL
451 || (errno == ERANGE && seed == ULONG_MAX))
452 {
453 Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
454 "in range [0; 4294967295]");
455 }
456 if (seed == 0) {
457 /* disable the randomized hash */
458 memset(secret, 0, secret_size);
459 }
460 else {
Christian Heimes985ecdc2013-11-20 11:46:18 +0100461 lcg_urandom(seed, secret, secret_size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100462 }
463 }
464 else {
465#ifdef MS_WINDOWS
Christian Heimes985ecdc2013-11-20 11:46:18 +0100466 (void)win32_urandom(secret, secret_size, 0);
Victor Stinnerbae2d622015-10-01 09:47:30 +0200467#elif defined(PY_GETENTROPY)
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100468 (void)py_getentropy(secret, secret_size, 1);
Christian Heimesaf01f662013-12-21 16:19:10 +0100469#else
Christian Heimes985ecdc2013-11-20 11:46:18 +0100470 dev_urandom_noraise(secret, secret_size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100471#endif
472 }
473}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200474
475void
476_PyRandom_Fini(void)
477{
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200478#ifdef MS_WINDOWS
479 if (hCryptProv) {
Tim Goldenb8ac3e12014-05-06 13:29:45 +0100480 CryptReleaseContext(hCryptProv, 0);
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200481 hCryptProv = 0;
482 }
Victor Stinnerbae2d622015-10-01 09:47:30 +0200483#elif defined(PY_GETENTROPY)
Victor Stinner4d6a3d62014-12-21 01:16:38 +0100484 /* nothing to clean */
Victor Stinnerd50c3f32014-05-02 22:06:44 +0200485#else
Antoine Pitrou4879a962013-08-31 00:26:02 +0200486 dev_urandom_close();
487#endif
488}