Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #ifdef MS_WINDOWS |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 3 | # include <windows.h> |
Martin Panter | d2f8747 | 2016-07-29 04:00:44 +0000 | [diff] [blame^] | 4 | /* All sample MSDN wincrypt programs include the header below. It is at least |
| 5 | * required with Min GW. */ |
| 6 | # include <wincrypt.h> |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 7 | #else |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 8 | # include <fcntl.h> |
| 9 | # ifdef HAVE_SYS_STAT_H |
| 10 | # include <sys/stat.h> |
| 11 | # endif |
Victor Stinner | dddf484 | 2016-06-07 11:21:42 +0200 | [diff] [blame] | 12 | # ifdef HAVE_LINUX_RANDOM_H |
| 13 | # include <linux/random.h> |
| 14 | # endif |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 15 | # ifdef HAVE_GETRANDOM |
| 16 | # include <sys/random.h> |
| 17 | # elif defined(HAVE_GETRANDOM_SYSCALL) |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 18 | # include <sys/syscall.h> |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 19 | # endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 20 | #endif |
| 21 | |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 22 | #ifdef Py_DEBUG |
| 23 | int _Py_HashSecret_Initialized = 0; |
| 24 | #else |
| 25 | static int _Py_HashSecret_Initialized = 0; |
| 26 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 27 | |
| 28 | #ifdef MS_WINDOWS |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 29 | static HCRYPTPROV hCryptProv = 0; |
| 30 | |
| 31 | static int |
| 32 | win32_urandom_init(int raise) |
| 33 | { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 34 | /* Acquire context */ |
Martin v. Löwis | 3f50bf6 | 2013-01-25 14:06:18 +0100 | [diff] [blame] | 35 | if (!CryptAcquireContext(&hCryptProv, NULL, NULL, |
| 36 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 37 | goto error; |
| 38 | |
| 39 | return 0; |
| 40 | |
| 41 | error: |
| 42 | if (raise) |
| 43 | PyErr_SetFromWindowsErr(0); |
| 44 | else |
| 45 | Py_FatalError("Failed to initialize Windows random API (CryptoGen)"); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | /* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen |
Victor Stinner | 4d6a3d6 | 2014-12-21 01:16:38 +0100 | [diff] [blame] | 50 | API. Return 0 on success, or raise an exception and return -1 on error. */ |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 51 | static int |
| 52 | win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise) |
| 53 | { |
| 54 | Py_ssize_t chunk; |
| 55 | |
| 56 | if (hCryptProv == 0) |
| 57 | { |
| 58 | if (win32_urandom_init(raise) == -1) |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | while (size > 0) |
| 63 | { |
| 64 | chunk = size > INT_MAX ? INT_MAX : size; |
Victor Stinner | 0c08346 | 2013-11-15 23:26:25 +0100 | [diff] [blame] | 65 | if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer)) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 66 | { |
| 67 | /* CryptGenRandom() failed */ |
| 68 | if (raise) |
| 69 | PyErr_SetFromWindowsErr(0); |
| 70 | else |
| 71 | Py_FatalError("Failed to initialized the randomized hash " |
| 72 | "secret using CryptoGen)"); |
| 73 | return -1; |
| 74 | } |
| 75 | buffer += chunk; |
| 76 | size -= chunk; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 80 | |
Martin Panter | 39b1025 | 2016-06-10 08:07:11 +0000 | [diff] [blame] | 81 | /* Issue #25003: Don't use getentropy() on Solaris (available since |
| 82 | * Solaris 11.3), it is blocking whereas os.urandom() should not block. */ |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 83 | #elif defined(HAVE_GETENTROPY) && !defined(sun) |
| 84 | #define PY_GETENTROPY 1 |
| 85 | |
Victor Stinner | 4d6a3d6 | 2014-12-21 01:16:38 +0100 | [diff] [blame] | 86 | /* Fill buffer with size pseudo-random bytes generated by getentropy(). |
| 87 | Return 0 on success, or raise an exception and return -1 on error. |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 88 | |
Victor Stinner | 4d6a3d6 | 2014-12-21 01:16:38 +0100 | [diff] [blame] | 89 | If fatal is nonzero, call Py_FatalError() instead of raising an exception |
| 90 | on error. */ |
| 91 | static int |
| 92 | py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal) |
| 93 | { |
| 94 | while (size > 0) { |
| 95 | Py_ssize_t len = Py_MIN(size, 256); |
Victor Stinner | 9aa1331 | 2015-03-30 11:18:30 +0200 | [diff] [blame] | 96 | int res; |
| 97 | |
| 98 | if (!fatal) { |
| 99 | Py_BEGIN_ALLOW_THREADS |
| 100 | res = getentropy(buffer, len); |
| 101 | Py_END_ALLOW_THREADS |
| 102 | |
| 103 | if (res < 0) { |
Victor Stinner | 4d6a3d6 | 2014-12-21 01:16:38 +0100 | [diff] [blame] | 104 | PyErr_SetFromErrno(PyExc_OSError); |
| 105 | return -1; |
| 106 | } |
| 107 | } |
Victor Stinner | 9aa1331 | 2015-03-30 11:18:30 +0200 | [diff] [blame] | 108 | else { |
| 109 | res = getentropy(buffer, len); |
| 110 | if (res < 0) |
| 111 | Py_FatalError("getentropy() failed"); |
| 112 | } |
| 113 | |
Victor Stinner | 4d6a3d6 | 2014-12-21 01:16:38 +0100 | [diff] [blame] | 114 | buffer += len; |
| 115 | size -= len; |
| 116 | } |
| 117 | return 0; |
| 118 | } |
| 119 | |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 120 | #else |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 121 | |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 122 | #if defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL) |
| 123 | #define PY_GETRANDOM 1 |
| 124 | |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 125 | static int |
| 126 | py_getrandom(void *buffer, Py_ssize_t size, int raise) |
| 127 | { |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 128 | /* Is getrandom() supported by the running kernel? |
| 129 | * Need Linux kernel 3.17 or newer, or Solaris 11.3 or newer */ |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 130 | static int getrandom_works = 1; |
Victor Stinner | dddf484 | 2016-06-07 11:21:42 +0200 | [diff] [blame] | 131 | |
| 132 | /* getrandom() on Linux will block if called before the kernel has |
| 133 | * initialized the urandom entropy pool. This will cause Python |
| 134 | * to hang on startup if called very early in the boot process - |
| 135 | * see https://bugs.python.org/issue26839. To avoid this, use the |
| 136 | * GRND_NONBLOCK flag. */ |
| 137 | const int flags = GRND_NONBLOCK; |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 138 | |
| 139 | char *dest; |
Victor Stinner | ec721f3 | 2016-06-16 23:53:47 +0200 | [diff] [blame] | 140 | long n; |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 141 | |
| 142 | if (!getrandom_works) |
| 143 | return 0; |
| 144 | |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 145 | dest = buffer; |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 146 | while (0 < size) { |
Victor Stinner | 9d24271 | 2016-04-12 22:28:49 +0200 | [diff] [blame] | 147 | #ifdef sun |
| 148 | /* Issue #26735: On Solaris, getrandom() is limited to returning up |
| 149 | to 1024 bytes */ |
| 150 | n = Py_MIN(size, 1024); |
| 151 | #else |
Victor Stinner | ec721f3 | 2016-06-16 23:53:47 +0200 | [diff] [blame] | 152 | n = Py_MIN(size, LONG_MAX); |
Victor Stinner | 9d24271 | 2016-04-12 22:28:49 +0200 | [diff] [blame] | 153 | #endif |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 154 | |
Victor Stinner | 9d24271 | 2016-04-12 22:28:49 +0200 | [diff] [blame] | 155 | errno = 0; |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 156 | #ifdef HAVE_GETRANDOM |
| 157 | if (raise) { |
| 158 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 159 | n = getrandom(dest, n, flags); |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 160 | Py_END_ALLOW_THREADS |
| 161 | } |
| 162 | else { |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 163 | n = getrandom(dest, n, flags); |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 164 | } |
| 165 | #else |
| 166 | /* On Linux, use the syscall() function because the GNU libc doesn't |
| 167 | * expose the Linux getrandom() syscall yet. See: |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 168 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */ |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 169 | if (raise) { |
| 170 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 171 | n = syscall(SYS_getrandom, dest, n, flags); |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 172 | Py_END_ALLOW_THREADS |
| 173 | } |
| 174 | else { |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 175 | n = syscall(SYS_getrandom, dest, n, flags); |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 176 | } |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 177 | #endif |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 178 | |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 179 | if (n < 0) { |
| 180 | if (errno == ENOSYS) { |
| 181 | getrandom_works = 0; |
| 182 | return 0; |
| 183 | } |
Victor Stinner | dddf484 | 2016-06-07 11:21:42 +0200 | [diff] [blame] | 184 | if (errno == EAGAIN) { |
| 185 | /* If we failed with EAGAIN, the entropy pool was |
| 186 | * uninitialized. In this case, we return failure to fall |
| 187 | * back to reading from /dev/urandom. |
| 188 | * |
| 189 | * Note: In this case the data read will not be random so |
| 190 | * should not be used for cryptographic purposes. Retaining |
| 191 | * the existing semantics for practical purposes. */ |
| 192 | getrandom_works = 0; |
| 193 | return 0; |
| 194 | } |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 195 | |
| 196 | if (errno == EINTR) { |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 197 | if (PyErr_CheckSignals()) { |
| 198 | if (!raise) |
| 199 | Py_FatalError("getrandom() interrupted by a signal"); |
| 200 | return -1; |
| 201 | } |
| 202 | /* retry getrandom() */ |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | if (raise) |
| 207 | PyErr_SetFromErrno(PyExc_OSError); |
| 208 | else |
| 209 | Py_FatalError("getrandom() failed"); |
| 210 | return -1; |
| 211 | } |
| 212 | |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 213 | dest += n; |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 214 | size -= n; |
| 215 | } |
| 216 | return 1; |
| 217 | } |
| 218 | #endif |
| 219 | |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 220 | static struct { |
| 221 | int fd; |
| 222 | dev_t st_dev; |
| 223 | ino_t st_ino; |
| 224 | } urandom_cache = { -1 }; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 225 | |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 226 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 227 | /* Read size bytes from /dev/urandom into buffer. |
| 228 | Call Py_FatalError() on error. */ |
| 229 | static void |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 230 | dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 231 | { |
| 232 | int fd; |
| 233 | Py_ssize_t n; |
| 234 | |
| 235 | assert (0 < size); |
| 236 | |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 237 | #ifdef PY_GETRANDOM |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 238 | if (py_getrandom(buffer, size, 0) == 1) |
| 239 | return; |
| 240 | /* getrandom() is not supported by the running kernel, fall back |
| 241 | * on reading /dev/urandom */ |
| 242 | #endif |
| 243 | |
Victor Stinner | c7cd12d | 2015-03-19 23:24:45 +0100 | [diff] [blame] | 244 | fd = _Py_open_noraise("/dev/urandom", O_RDONLY); |
| 245 | if (fd < 0) |
| 246 | Py_FatalError("Failed to open /dev/urandom"); |
| 247 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 248 | while (0 < size) |
| 249 | { |
| 250 | do { |
| 251 | n = read(fd, buffer, (size_t)size); |
| 252 | } while (n < 0 && errno == EINTR); |
| 253 | if (n <= 0) |
| 254 | { |
| 255 | /* stop on error or if read(size) returned 0 */ |
| 256 | Py_FatalError("Failed to read bytes from /dev/urandom"); |
| 257 | break; |
| 258 | } |
| 259 | buffer += n; |
Victor Stinner | c72828b | 2016-06-14 16:35:49 +0200 | [diff] [blame] | 260 | size -= n; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 261 | } |
| 262 | close(fd); |
| 263 | } |
| 264 | |
| 265 | /* Read size bytes from /dev/urandom into buffer. |
| 266 | Return 0 on success, raise an exception and return -1 on error. */ |
| 267 | static int |
| 268 | dev_urandom_python(char *buffer, Py_ssize_t size) |
| 269 | { |
| 270 | int fd; |
| 271 | Py_ssize_t n; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 272 | struct _Py_stat_struct st; |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 273 | #ifdef PY_GETRANDOM |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 274 | int res; |
| 275 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 276 | |
| 277 | if (size <= 0) |
| 278 | return 0; |
| 279 | |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 280 | #ifdef PY_GETRANDOM |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 281 | res = py_getrandom(buffer, size, 1); |
| 282 | if (res < 0) |
| 283 | return -1; |
| 284 | if (res == 1) |
| 285 | return 0; |
| 286 | /* getrandom() is not supported by the running kernel, fall back |
| 287 | * on reading /dev/urandom */ |
| 288 | #endif |
| 289 | |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 290 | if (urandom_cache.fd >= 0) { |
| 291 | /* Does the fd point to the same thing as before? (issue #21207) */ |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 292 | if (_Py_fstat_noraise(urandom_cache.fd, &st) |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 293 | || st.st_dev != urandom_cache.st_dev |
| 294 | || st.st_ino != urandom_cache.st_ino) { |
| 295 | /* Something changed: forget the cached fd (but don't close it, |
| 296 | since it probably points to something important for some |
| 297 | third-party code). */ |
| 298 | urandom_cache.fd = -1; |
| 299 | } |
| 300 | } |
| 301 | if (urandom_cache.fd >= 0) |
| 302 | fd = urandom_cache.fd; |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 303 | else { |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 304 | fd = _Py_open("/dev/urandom", O_RDONLY); |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 305 | if (fd < 0) { |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 306 | if (errno == ENOENT || errno == ENXIO || |
| 307 | errno == ENODEV || errno == EACCES) |
| 308 | PyErr_SetString(PyExc_NotImplementedError, |
| 309 | "/dev/urandom (or equivalent) not found"); |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 310 | /* otherwise, keep the OSError exception raised by _Py_open() */ |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 311 | return -1; |
| 312 | } |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 313 | if (urandom_cache.fd >= 0) { |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 314 | /* urandom_fd was initialized by another thread while we were |
| 315 | not holding the GIL, keep it. */ |
| 316 | close(fd); |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 317 | fd = urandom_cache.fd; |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 318 | } |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 319 | else { |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 320 | if (_Py_fstat(fd, &st)) { |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 321 | close(fd); |
| 322 | return -1; |
| 323 | } |
| 324 | else { |
| 325 | urandom_cache.fd = fd; |
| 326 | urandom_cache.st_dev = st.st_dev; |
| 327 | urandom_cache.st_ino = st.st_ino; |
| 328 | } |
| 329 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 332 | do { |
Victor Stinner | c9382eb | 2015-03-19 23:36:33 +0100 | [diff] [blame] | 333 | n = _Py_read(fd, buffer, (size_t)size); |
| 334 | if (n == -1) |
| 335 | return -1; |
| 336 | if (n == 0) { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 337 | PyErr_Format(PyExc_RuntimeError, |
Victor Stinner | c9382eb | 2015-03-19 23:36:33 +0100 | [diff] [blame] | 338 | "Failed to read %zi bytes from /dev/urandom", |
| 339 | size); |
| 340 | return -1; |
| 341 | } |
| 342 | |
| 343 | buffer += n; |
| 344 | size -= n; |
| 345 | } while (0 < size); |
| 346 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | } |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 349 | |
| 350 | static void |
| 351 | dev_urandom_close(void) |
| 352 | { |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 353 | if (urandom_cache.fd >= 0) { |
| 354 | close(urandom_cache.fd); |
| 355 | urandom_cache.fd = -1; |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 359 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 360 | |
| 361 | /* Fill buffer with pseudo-random bytes generated by a linear congruent |
| 362 | generator (LCG): |
| 363 | |
| 364 | x(n+1) = (x(n) * 214013 + 2531011) % 2^32 |
| 365 | |
| 366 | Use bits 23..16 of x(n) to generate a byte. */ |
| 367 | static void |
| 368 | lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size) |
| 369 | { |
| 370 | size_t index; |
| 371 | unsigned int x; |
| 372 | |
| 373 | x = x0; |
| 374 | for (index=0; index < size; index++) { |
| 375 | x *= 214013; |
| 376 | x += 2531011; |
| 377 | /* modulo 2 ^ (8 * sizeof(int)) */ |
| 378 | buffer[index] = (x >> 16) & 0xff; |
| 379 | } |
| 380 | } |
| 381 | |
Georg Brandl | c6a2c9b | 2013-10-06 18:43:19 +0200 | [diff] [blame] | 382 | /* Fill buffer with size pseudo-random bytes from the operating system random |
Serhiy Storchaka | 56a6d85 | 2014-12-01 18:28:43 +0200 | [diff] [blame] | 383 | number generator (RNG). It is suitable for most cryptographic purposes |
Georg Brandl | c6a2c9b | 2013-10-06 18:43:19 +0200 | [diff] [blame] | 384 | except long living private keys for asymmetric encryption. |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 385 | |
| 386 | Return 0 on success, raise an exception and return -1 on error. */ |
| 387 | int |
| 388 | _PyOS_URandom(void *buffer, Py_ssize_t size) |
| 389 | { |
| 390 | if (size < 0) { |
| 391 | PyErr_Format(PyExc_ValueError, |
| 392 | "negative argument not allowed"); |
| 393 | return -1; |
| 394 | } |
| 395 | if (size == 0) |
| 396 | return 0; |
| 397 | |
| 398 | #ifdef MS_WINDOWS |
| 399 | return win32_urandom((unsigned char *)buffer, size, 1); |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 400 | #elif defined(PY_GETENTROPY) |
Victor Stinner | 4d6a3d6 | 2014-12-21 01:16:38 +0100 | [diff] [blame] | 401 | return py_getentropy(buffer, size, 0); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 402 | #else |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 403 | return dev_urandom_python((char*)buffer, size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 404 | #endif |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | _PyRandom_Init(void) |
| 409 | { |
| 410 | char *env; |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 411 | unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc; |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 412 | Py_ssize_t secret_size = sizeof(_Py_HashSecret_t); |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 413 | Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc)); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 414 | |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 415 | if (_Py_HashSecret_Initialized) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 416 | return; |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 417 | _Py_HashSecret_Initialized = 1; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 418 | |
| 419 | /* |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 420 | Hash randomization is enabled. Generate a per-process secret, |
| 421 | using PYTHONHASHSEED if provided. |
| 422 | */ |
| 423 | |
| 424 | env = Py_GETENV("PYTHONHASHSEED"); |
Georg Brandl | 12897d7 | 2012-02-20 23:49:29 +0100 | [diff] [blame] | 425 | if (env && *env != '\0' && strcmp(env, "random") != 0) { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 426 | char *endptr = env; |
| 427 | unsigned long seed; |
| 428 | seed = strtoul(env, &endptr, 10); |
| 429 | if (*endptr != '\0' |
| 430 | || seed > 4294967295UL |
| 431 | || (errno == ERANGE && seed == ULONG_MAX)) |
| 432 | { |
| 433 | Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer " |
| 434 | "in range [0; 4294967295]"); |
| 435 | } |
| 436 | if (seed == 0) { |
| 437 | /* disable the randomized hash */ |
| 438 | memset(secret, 0, secret_size); |
| 439 | } |
| 440 | else { |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 441 | lcg_urandom(seed, secret, secret_size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | else { |
| 445 | #ifdef MS_WINDOWS |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 446 | (void)win32_urandom(secret, secret_size, 0); |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 447 | #elif defined(PY_GETENTROPY) |
Victor Stinner | 4d6a3d6 | 2014-12-21 01:16:38 +0100 | [diff] [blame] | 448 | (void)py_getentropy(secret, secret_size, 1); |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 449 | #else |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 450 | dev_urandom_noraise(secret, secret_size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 451 | #endif |
| 452 | } |
| 453 | } |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 454 | |
| 455 | void |
| 456 | _PyRandom_Fini(void) |
| 457 | { |
Victor Stinner | d50c3f3 | 2014-05-02 22:06:44 +0200 | [diff] [blame] | 458 | #ifdef MS_WINDOWS |
| 459 | if (hCryptProv) { |
Tim Golden | b8ac3e1 | 2014-05-06 13:29:45 +0100 | [diff] [blame] | 460 | CryptReleaseContext(hCryptProv, 0); |
Victor Stinner | d50c3f3 | 2014-05-02 22:06:44 +0200 | [diff] [blame] | 461 | hCryptProv = 0; |
| 462 | } |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 463 | #elif defined(PY_GETENTROPY) |
Victor Stinner | 4d6a3d6 | 2014-12-21 01:16:38 +0100 | [diff] [blame] | 464 | /* nothing to clean */ |
Victor Stinner | d50c3f3 | 2014-05-02 22:06:44 +0200 | [diff] [blame] | 465 | #else |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 466 | dev_urandom_close(); |
| 467 | #endif |
| 468 | } |