Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #ifdef MS_WINDOWS |
| 3 | #include <windows.h> |
| 4 | #else |
| 5 | #include <fcntl.h> |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 6 | #ifdef HAVE_SYS_STAT_H |
| 7 | #include <sys/stat.h> |
| 8 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 9 | #endif |
| 10 | |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 11 | #ifdef Py_DEBUG |
| 12 | int _Py_HashSecret_Initialized = 0; |
| 13 | #else |
| 14 | static int _Py_HashSecret_Initialized = 0; |
| 15 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 16 | |
| 17 | #ifdef MS_WINDOWS |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 18 | /* This handle is never explicitly released. Instead, the operating |
| 19 | system will release it when the process terminates. */ |
| 20 | static HCRYPTPROV hCryptProv = 0; |
| 21 | |
| 22 | static int |
| 23 | win32_urandom_init(int raise) |
| 24 | { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 25 | /* Acquire context */ |
Martin v. Löwis | 3f50bf6 | 2013-01-25 14:06:18 +0100 | [diff] [blame] | 26 | if (!CryptAcquireContext(&hCryptProv, NULL, NULL, |
| 27 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 28 | goto error; |
| 29 | |
| 30 | return 0; |
| 31 | |
| 32 | error: |
| 33 | if (raise) |
| 34 | PyErr_SetFromWindowsErr(0); |
| 35 | else |
| 36 | Py_FatalError("Failed to initialize Windows random API (CryptoGen)"); |
| 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | /* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen |
| 41 | API. Return 0 on success, or -1 on error. */ |
| 42 | static int |
| 43 | win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise) |
| 44 | { |
| 45 | Py_ssize_t chunk; |
| 46 | |
| 47 | if (hCryptProv == 0) |
| 48 | { |
| 49 | if (win32_urandom_init(raise) == -1) |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | while (size > 0) |
| 54 | { |
| 55 | chunk = size > INT_MAX ? INT_MAX : size; |
Victor Stinner | 0c08346 | 2013-11-15 23:26:25 +0100 | [diff] [blame] | 56 | if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer)) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 57 | { |
| 58 | /* CryptGenRandom() failed */ |
| 59 | if (raise) |
| 60 | PyErr_SetFromWindowsErr(0); |
| 61 | else |
| 62 | Py_FatalError("Failed to initialized the randomized hash " |
| 63 | "secret using CryptoGen)"); |
| 64 | return -1; |
| 65 | } |
| 66 | buffer += chunk; |
| 67 | size -= chunk; |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | #endif /* MS_WINDOWS */ |
| 72 | |
| 73 | |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 74 | #ifndef MS_WINDOWS |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 75 | static struct { |
| 76 | int fd; |
| 77 | dev_t st_dev; |
| 78 | ino_t st_ino; |
| 79 | } urandom_cache = { -1 }; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 80 | |
| 81 | /* Read size bytes from /dev/urandom into buffer. |
| 82 | Call Py_FatalError() on error. */ |
| 83 | static void |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 84 | dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 85 | { |
| 86 | int fd; |
| 87 | Py_ssize_t n; |
| 88 | |
| 89 | assert (0 < size); |
| 90 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 91 | fd = _Py_open("/dev/urandom", O_RDONLY); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 92 | if (fd < 0) |
| 93 | Py_FatalError("Failed to open /dev/urandom"); |
| 94 | |
| 95 | while (0 < size) |
| 96 | { |
| 97 | do { |
| 98 | n = read(fd, buffer, (size_t)size); |
| 99 | } while (n < 0 && errno == EINTR); |
| 100 | if (n <= 0) |
| 101 | { |
| 102 | /* stop on error or if read(size) returned 0 */ |
| 103 | Py_FatalError("Failed to read bytes from /dev/urandom"); |
| 104 | break; |
| 105 | } |
| 106 | buffer += n; |
| 107 | size -= (Py_ssize_t)n; |
| 108 | } |
| 109 | close(fd); |
| 110 | } |
| 111 | |
| 112 | /* Read size bytes from /dev/urandom into buffer. |
| 113 | Return 0 on success, raise an exception and return -1 on error. */ |
| 114 | static int |
| 115 | dev_urandom_python(char *buffer, Py_ssize_t size) |
| 116 | { |
| 117 | int fd; |
| 118 | Py_ssize_t n; |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 119 | struct stat st; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 120 | |
| 121 | if (size <= 0) |
| 122 | return 0; |
| 123 | |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 124 | if (urandom_cache.fd >= 0) { |
| 125 | /* Does the fd point to the same thing as before? (issue #21207) */ |
| 126 | if (fstat(urandom_cache.fd, &st) |
| 127 | || st.st_dev != urandom_cache.st_dev |
| 128 | || st.st_ino != urandom_cache.st_ino) { |
| 129 | /* Something changed: forget the cached fd (but don't close it, |
| 130 | since it probably points to something important for some |
| 131 | third-party code). */ |
| 132 | urandom_cache.fd = -1; |
| 133 | } |
| 134 | } |
| 135 | if (urandom_cache.fd >= 0) |
| 136 | fd = urandom_cache.fd; |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 137 | else { |
| 138 | Py_BEGIN_ALLOW_THREADS |
| 139 | fd = _Py_open("/dev/urandom", O_RDONLY); |
| 140 | Py_END_ALLOW_THREADS |
| 141 | if (fd < 0) |
| 142 | { |
| 143 | if (errno == ENOENT || errno == ENXIO || |
| 144 | errno == ENODEV || errno == EACCES) |
| 145 | PyErr_SetString(PyExc_NotImplementedError, |
| 146 | "/dev/urandom (or equivalent) not found"); |
| 147 | else |
| 148 | PyErr_SetFromErrno(PyExc_OSError); |
| 149 | return -1; |
| 150 | } |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 151 | if (urandom_cache.fd >= 0) { |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 152 | /* urandom_fd was initialized by another thread while we were |
| 153 | not holding the GIL, keep it. */ |
| 154 | close(fd); |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 155 | fd = urandom_cache.fd; |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 156 | } |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 157 | else { |
| 158 | if (fstat(fd, &st)) { |
| 159 | PyErr_SetFromErrno(PyExc_OSError); |
| 160 | close(fd); |
| 161 | return -1; |
| 162 | } |
| 163 | else { |
| 164 | urandom_cache.fd = fd; |
| 165 | urandom_cache.st_dev = st.st_dev; |
| 166 | urandom_cache.st_ino = st.st_ino; |
| 167 | } |
| 168 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | Py_BEGIN_ALLOW_THREADS |
| 172 | do { |
| 173 | do { |
| 174 | n = read(fd, buffer, (size_t)size); |
| 175 | } while (n < 0 && errno == EINTR); |
| 176 | if (n <= 0) |
| 177 | break; |
| 178 | buffer += n; |
| 179 | size -= (Py_ssize_t)n; |
| 180 | } while (0 < size); |
| 181 | Py_END_ALLOW_THREADS |
| 182 | |
| 183 | if (n <= 0) |
| 184 | { |
| 185 | /* stop on error or if read(size) returned 0 */ |
| 186 | if (n < 0) |
| 187 | PyErr_SetFromErrno(PyExc_OSError); |
| 188 | else |
| 189 | PyErr_Format(PyExc_RuntimeError, |
| 190 | "Failed to read %zi bytes from /dev/urandom", |
| 191 | size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 192 | return -1; |
| 193 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 194 | return 0; |
| 195 | } |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 196 | |
| 197 | static void |
| 198 | dev_urandom_close(void) |
| 199 | { |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 200 | if (urandom_cache.fd >= 0) { |
| 201 | close(urandom_cache.fd); |
| 202 | urandom_cache.fd = -1; |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 206 | #endif /* MS_WINDOWS */ |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 207 | |
| 208 | /* Fill buffer with pseudo-random bytes generated by a linear congruent |
| 209 | generator (LCG): |
| 210 | |
| 211 | x(n+1) = (x(n) * 214013 + 2531011) % 2^32 |
| 212 | |
| 213 | Use bits 23..16 of x(n) to generate a byte. */ |
| 214 | static void |
| 215 | lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size) |
| 216 | { |
| 217 | size_t index; |
| 218 | unsigned int x; |
| 219 | |
| 220 | x = x0; |
| 221 | for (index=0; index < size; index++) { |
| 222 | x *= 214013; |
| 223 | x += 2531011; |
| 224 | /* modulo 2 ^ (8 * sizeof(int)) */ |
| 225 | buffer[index] = (x >> 16) & 0xff; |
| 226 | } |
| 227 | } |
| 228 | |
Georg Brandl | c6a2c9b | 2013-10-06 18:43:19 +0200 | [diff] [blame] | 229 | /* Fill buffer with size pseudo-random bytes from the operating system random |
| 230 | number generator (RNG). It is suitable for for most cryptographic purposes |
| 231 | except long living private keys for asymmetric encryption. |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 232 | |
| 233 | Return 0 on success, raise an exception and return -1 on error. */ |
| 234 | int |
| 235 | _PyOS_URandom(void *buffer, Py_ssize_t size) |
| 236 | { |
| 237 | if (size < 0) { |
| 238 | PyErr_Format(PyExc_ValueError, |
| 239 | "negative argument not allowed"); |
| 240 | return -1; |
| 241 | } |
| 242 | if (size == 0) |
| 243 | return 0; |
| 244 | |
| 245 | #ifdef MS_WINDOWS |
| 246 | return win32_urandom((unsigned char *)buffer, size, 1); |
| 247 | #else |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 248 | return dev_urandom_python((char*)buffer, size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 249 | #endif |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | _PyRandom_Init(void) |
| 254 | { |
| 255 | char *env; |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 256 | unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc; |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 257 | Py_ssize_t secret_size = sizeof(_Py_HashSecret_t); |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 258 | assert(secret_size == sizeof(_Py_HashSecret.uc)); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 259 | |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 260 | if (_Py_HashSecret_Initialized) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 261 | return; |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 262 | _Py_HashSecret_Initialized = 1; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 263 | |
| 264 | /* |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 265 | Hash randomization is enabled. Generate a per-process secret, |
| 266 | using PYTHONHASHSEED if provided. |
| 267 | */ |
| 268 | |
| 269 | env = Py_GETENV("PYTHONHASHSEED"); |
Georg Brandl | 12897d7 | 2012-02-20 23:49:29 +0100 | [diff] [blame] | 270 | if (env && *env != '\0' && strcmp(env, "random") != 0) { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 271 | char *endptr = env; |
| 272 | unsigned long seed; |
| 273 | seed = strtoul(env, &endptr, 10); |
| 274 | if (*endptr != '\0' |
| 275 | || seed > 4294967295UL |
| 276 | || (errno == ERANGE && seed == ULONG_MAX)) |
| 277 | { |
| 278 | Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer " |
| 279 | "in range [0; 4294967295]"); |
| 280 | } |
| 281 | if (seed == 0) { |
| 282 | /* disable the randomized hash */ |
| 283 | memset(secret, 0, secret_size); |
| 284 | } |
| 285 | else { |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 286 | lcg_urandom(seed, secret, secret_size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | else { |
| 290 | #ifdef MS_WINDOWS |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 291 | (void)win32_urandom(secret, secret_size, 0); |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 292 | #else |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 293 | dev_urandom_noraise(secret, secret_size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 294 | #endif |
| 295 | } |
| 296 | } |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 297 | |
| 298 | void |
| 299 | _PyRandom_Fini(void) |
| 300 | { |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 301 | #ifndef MS_WINDOWS |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 302 | dev_urandom_close(); |
| 303 | #endif |
| 304 | } |