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