Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #ifdef MS_WINDOWS |
| 3 | #include <windows.h> |
| 4 | #else |
| 5 | #include <fcntl.h> |
| 6 | #endif |
| 7 | |
Benjamin Peterson | 26da920 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 8 | #ifdef Py_DEBUG |
| 9 | int _Py_HashSecret_Initialized = 0; |
| 10 | #else |
| 11 | static int _Py_HashSecret_Initialized = 0; |
| 12 | #endif |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 13 | |
| 14 | #ifdef MS_WINDOWS |
| 15 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\ |
| 16 | LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\ |
| 17 | DWORD dwFlags ); |
| 18 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\ |
| 19 | BYTE *pbBuffer ); |
| 20 | |
| 21 | static CRYPTGENRANDOM pCryptGenRandom = NULL; |
| 22 | /* This handle is never explicitly released. Instead, the operating |
| 23 | system will release it when the process terminates. */ |
| 24 | static HCRYPTPROV hCryptProv = 0; |
| 25 | |
| 26 | static int |
| 27 | win32_urandom_init(int raise) |
| 28 | { |
| 29 | HINSTANCE hAdvAPI32 = NULL; |
| 30 | CRYPTACQUIRECONTEXTA pCryptAcquireContext = NULL; |
| 31 | |
| 32 | /* Obtain handle to the DLL containing CryptoAPI. This should not fail. */ |
| 33 | hAdvAPI32 = GetModuleHandle("advapi32.dll"); |
| 34 | if(hAdvAPI32 == NULL) |
| 35 | goto error; |
| 36 | |
| 37 | /* Obtain pointers to the CryptoAPI functions. This will fail on some early |
| 38 | versions of Win95. */ |
| 39 | pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress( |
| 40 | hAdvAPI32, "CryptAcquireContextA"); |
| 41 | if (pCryptAcquireContext == NULL) |
| 42 | goto error; |
| 43 | |
| 44 | pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(hAdvAPI32, |
| 45 | "CryptGenRandom"); |
| 46 | if (pCryptGenRandom == NULL) |
| 47 | goto error; |
| 48 | |
| 49 | /* Acquire context */ |
| 50 | if (! pCryptAcquireContext(&hCryptProv, NULL, NULL, |
| 51 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 52 | goto error; |
| 53 | |
| 54 | return 0; |
| 55 | |
| 56 | error: |
| 57 | if (raise) |
| 58 | PyErr_SetFromWindowsErr(0); |
| 59 | else |
| 60 | Py_FatalError("Failed to initialize Windows random API (CryptoGen)"); |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | /* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen |
| 65 | API. Return 0 on success, or -1 on error. */ |
| 66 | static int |
| 67 | win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise) |
| 68 | { |
| 69 | Py_ssize_t chunk; |
| 70 | |
| 71 | if (hCryptProv == 0) |
| 72 | { |
| 73 | if (win32_urandom_init(raise) == -1) |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | while (size > 0) |
| 78 | { |
| 79 | chunk = size > INT_MAX ? INT_MAX : size; |
| 80 | if (!pCryptGenRandom(hCryptProv, chunk, buffer)) |
| 81 | { |
| 82 | /* CryptGenRandom() failed */ |
| 83 | if (raise) |
| 84 | PyErr_SetFromWindowsErr(0); |
| 85 | else |
| 86 | Py_FatalError("Failed to initialized the randomized hash " |
| 87 | "secret using CryptoGen)"); |
| 88 | return -1; |
| 89 | } |
| 90 | buffer += chunk; |
| 91 | size -= chunk; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 95 | |
Benjamin Peterson | 27c269a | 2014-12-26 11:09:00 -0600 | [diff] [blame] | 96 | #elif HAVE_GETENTROPY |
| 97 | /* Fill buffer with size pseudo-random bytes generated by getentropy(). |
| 98 | Return 0 on success, or raise an exception and return -1 on error. |
| 99 | If fatal is nonzero, call Py_FatalError() instead of raising an exception |
| 100 | on error. */ |
| 101 | static int |
| 102 | py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal) |
| 103 | { |
| 104 | while (size > 0) { |
Serhiy Storchaka | e8d750c | 2015-02-16 08:05:10 +0200 | [diff] [blame] | 105 | Py_ssize_t len = size < 256 ? size : 256; |
Victor Stinner | e993245 | 2015-03-30 11:22:13 +0200 | [diff] [blame] | 106 | int res; |
| 107 | |
| 108 | if (!fatal) { |
| 109 | Py_BEGIN_ALLOW_THREADS |
| 110 | res = getentropy(buffer, len); |
| 111 | Py_END_ALLOW_THREADS |
| 112 | |
| 113 | if (res < 0) { |
Benjamin Peterson | 27c269a | 2014-12-26 11:09:00 -0600 | [diff] [blame] | 114 | PyErr_SetFromErrno(PyExc_OSError); |
| 115 | return -1; |
| 116 | } |
| 117 | } |
Victor Stinner | e993245 | 2015-03-30 11:22:13 +0200 | [diff] [blame] | 118 | else { |
| 119 | res = getentropy(buffer, len); |
| 120 | if (res < 0) |
| 121 | Py_FatalError("getentropy() failed"); |
| 122 | } |
| 123 | |
Benjamin Peterson | 27c269a | 2014-12-26 11:09:00 -0600 | [diff] [blame] | 124 | buffer += len; |
| 125 | size -= len; |
| 126 | } |
| 127 | return 0; |
| 128 | } |
| 129 | #endif |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 130 | |
| 131 | #ifdef __VMS |
| 132 | /* Use openssl random routine */ |
| 133 | #include <openssl/rand.h> |
| 134 | static int |
| 135 | vms_urandom(unsigned char *buffer, Py_ssize_t size, int raise) |
| 136 | { |
| 137 | if (RAND_pseudo_bytes(buffer, size) < 0) { |
| 138 | if (raise) { |
| 139 | PyErr_Format(PyExc_ValueError, |
| 140 | "RAND_pseudo_bytes"); |
| 141 | } else { |
| 142 | Py_FatalError("Failed to initialize the randomized hash " |
| 143 | "secret using RAND_pseudo_bytes"); |
| 144 | } |
| 145 | return -1; |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | #endif /* __VMS */ |
| 150 | |
| 151 | |
| 152 | #if !defined(MS_WINDOWS) && !defined(__VMS) |
| 153 | |
Benjamin Peterson | 57057a6 | 2014-08-28 12:30:00 -0400 | [diff] [blame] | 154 | static struct { |
| 155 | int fd; |
| 156 | dev_t st_dev; |
| 157 | ino_t st_ino; |
| 158 | } urandom_cache = { -1 }; |
| 159 | |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 160 | /* Read size bytes from /dev/urandom into buffer. |
| 161 | Call Py_FatalError() on error. */ |
| 162 | static void |
Benjamin Peterson | 57057a6 | 2014-08-28 12:30:00 -0400 | [diff] [blame] | 163 | dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size) |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 164 | { |
| 165 | int fd; |
| 166 | Py_ssize_t n; |
| 167 | |
| 168 | assert (0 < size); |
| 169 | |
| 170 | fd = open("/dev/urandom", O_RDONLY); |
| 171 | if (fd < 0) |
| 172 | Py_FatalError("Failed to open /dev/urandom"); |
| 173 | |
| 174 | while (0 < size) |
| 175 | { |
| 176 | do { |
| 177 | n = read(fd, buffer, (size_t)size); |
| 178 | } while (n < 0 && errno == EINTR); |
| 179 | if (n <= 0) |
| 180 | { |
| 181 | /* stop on error or if read(size) returned 0 */ |
| 182 | Py_FatalError("Failed to read bytes from /dev/urandom"); |
| 183 | break; |
| 184 | } |
| 185 | buffer += n; |
| 186 | size -= (Py_ssize_t)n; |
| 187 | } |
| 188 | close(fd); |
| 189 | } |
| 190 | |
| 191 | /* Read size bytes from /dev/urandom into buffer. |
| 192 | Return 0 on success, raise an exception and return -1 on error. */ |
| 193 | static int |
| 194 | dev_urandom_python(char *buffer, Py_ssize_t size) |
| 195 | { |
| 196 | int fd; |
| 197 | Py_ssize_t n; |
Benjamin Peterson | 57057a6 | 2014-08-28 12:30:00 -0400 | [diff] [blame] | 198 | struct stat st; |
Victor Stinner | e0a0bd6 | 2015-02-24 14:30:43 +0100 | [diff] [blame] | 199 | int attr; |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 200 | |
| 201 | if (size <= 0) |
| 202 | return 0; |
| 203 | |
Benjamin Peterson | 57057a6 | 2014-08-28 12:30:00 -0400 | [diff] [blame] | 204 | if (urandom_cache.fd >= 0) { |
| 205 | /* Does the fd point to the same thing as before? (issue #21207) */ |
| 206 | if (fstat(urandom_cache.fd, &st) |
| 207 | || st.st_dev != urandom_cache.st_dev |
| 208 | || st.st_ino != urandom_cache.st_ino) { |
| 209 | /* Something changed: forget the cached fd (but don't close it, |
| 210 | since it probably points to something important for some |
| 211 | third-party code). */ |
| 212 | urandom_cache.fd = -1; |
| 213 | } |
| 214 | } |
| 215 | if (urandom_cache.fd >= 0) |
| 216 | fd = urandom_cache.fd; |
| 217 | else { |
| 218 | Py_BEGIN_ALLOW_THREADS |
| 219 | fd = open("/dev/urandom", O_RDONLY); |
| 220 | Py_END_ALLOW_THREADS |
| 221 | if (fd < 0) |
| 222 | { |
| 223 | if (errno == ENOENT || errno == ENXIO || |
| 224 | errno == ENODEV || errno == EACCES) |
| 225 | PyErr_SetString(PyExc_NotImplementedError, |
| 226 | "/dev/urandom (or equivalent) not found"); |
| 227 | else |
| 228 | PyErr_SetFromErrno(PyExc_OSError); |
| 229 | return -1; |
| 230 | } |
Victor Stinner | e0a0bd6 | 2015-02-24 14:30:43 +0100 | [diff] [blame] | 231 | |
| 232 | /* try to make the file descriptor non-inheritable, ignore errors */ |
| 233 | attr = fcntl(fd, F_GETFD); |
| 234 | if (attr >= 0) { |
| 235 | attr |= FD_CLOEXEC; |
| 236 | (void)fcntl(fd, F_SETFD, attr); |
| 237 | } |
| 238 | |
Benjamin Peterson | 57057a6 | 2014-08-28 12:30:00 -0400 | [diff] [blame] | 239 | if (urandom_cache.fd >= 0) { |
| 240 | /* urandom_fd was initialized by another thread while we were |
| 241 | not holding the GIL, keep it. */ |
| 242 | close(fd); |
| 243 | fd = urandom_cache.fd; |
| 244 | } |
| 245 | else { |
| 246 | if (fstat(fd, &st)) { |
| 247 | PyErr_SetFromErrno(PyExc_OSError); |
| 248 | close(fd); |
| 249 | return -1; |
| 250 | } |
| 251 | else { |
| 252 | urandom_cache.fd = fd; |
| 253 | urandom_cache.st_dev = st.st_dev; |
| 254 | urandom_cache.st_ino = st.st_ino; |
| 255 | } |
| 256 | } |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | Py_BEGIN_ALLOW_THREADS |
| 260 | do { |
| 261 | do { |
| 262 | n = read(fd, buffer, (size_t)size); |
| 263 | } while (n < 0 && errno == EINTR); |
| 264 | if (n <= 0) |
| 265 | break; |
| 266 | buffer += n; |
| 267 | size -= (Py_ssize_t)n; |
| 268 | } while (0 < size); |
| 269 | Py_END_ALLOW_THREADS |
| 270 | |
| 271 | if (n <= 0) |
| 272 | { |
| 273 | /* stop on error or if read(size) returned 0 */ |
| 274 | if (n < 0) |
| 275 | PyErr_SetFromErrno(PyExc_OSError); |
| 276 | else |
| 277 | PyErr_Format(PyExc_RuntimeError, |
| 278 | "Failed to read %zi bytes from /dev/urandom", |
| 279 | size); |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 280 | return -1; |
| 281 | } |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 282 | return 0; |
| 283 | } |
Benjamin Peterson | 57057a6 | 2014-08-28 12:30:00 -0400 | [diff] [blame] | 284 | |
| 285 | static void |
| 286 | dev_urandom_close(void) |
| 287 | { |
| 288 | if (urandom_cache.fd >= 0) { |
| 289 | close(urandom_cache.fd); |
| 290 | urandom_cache.fd = -1; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 295 | #endif /* !defined(MS_WINDOWS) && !defined(__VMS) */ |
| 296 | |
| 297 | /* Fill buffer with pseudo-random bytes generated by a linear congruent |
| 298 | generator (LCG): |
| 299 | |
| 300 | x(n+1) = (x(n) * 214013 + 2531011) % 2^32 |
| 301 | |
| 302 | Use bits 23..16 of x(n) to generate a byte. */ |
| 303 | static void |
| 304 | lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size) |
| 305 | { |
| 306 | size_t index; |
| 307 | unsigned int x; |
| 308 | |
| 309 | x = x0; |
| 310 | for (index=0; index < size; index++) { |
| 311 | x *= 214013; |
| 312 | x += 2531011; |
| 313 | /* modulo 2 ^ (8 * sizeof(int)) */ |
| 314 | buffer[index] = (x >> 16) & 0xff; |
| 315 | } |
| 316 | } |
| 317 | |
Georg Brandl | c0edade | 2013-10-06 18:43:19 +0200 | [diff] [blame] | 318 | /* Fill buffer with size pseudo-random bytes from the operating system random |
Serhiy Storchaka | 0f8f784 | 2014-12-01 18:16:30 +0200 | [diff] [blame] | 319 | number generator (RNG). It is suitable for most cryptographic purposes |
Georg Brandl | c0edade | 2013-10-06 18:43:19 +0200 | [diff] [blame] | 320 | except long living private keys for asymmetric encryption. |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 321 | |
| 322 | Return 0 on success, raise an exception and return -1 on error. */ |
| 323 | int |
| 324 | _PyOS_URandom(void *buffer, Py_ssize_t size) |
| 325 | { |
| 326 | if (size < 0) { |
| 327 | PyErr_Format(PyExc_ValueError, |
| 328 | "negative argument not allowed"); |
| 329 | return -1; |
| 330 | } |
| 331 | if (size == 0) |
| 332 | return 0; |
| 333 | |
| 334 | #ifdef MS_WINDOWS |
| 335 | return win32_urandom((unsigned char *)buffer, size, 1); |
Benjamin Peterson | 27c269a | 2014-12-26 11:09:00 -0600 | [diff] [blame] | 336 | #elif HAVE_GETENTROPY |
| 337 | return py_getentropy(buffer, size, 0); |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 338 | #else |
| 339 | # ifdef __VMS |
| 340 | return vms_urandom((unsigned char *)buffer, size, 1); |
| 341 | # else |
| 342 | return dev_urandom_python((char*)buffer, size); |
| 343 | # endif |
| 344 | #endif |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | _PyRandom_Init(void) |
| 349 | { |
| 350 | char *env; |
| 351 | void *secret = &_Py_HashSecret; |
Benjamin Peterson | 26da920 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 352 | Py_ssize_t secret_size = sizeof(_Py_HashSecret_t); |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 353 | |
Benjamin Peterson | 26da920 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 354 | if (_Py_HashSecret_Initialized) |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 355 | return; |
Benjamin Peterson | 26da920 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 356 | _Py_HashSecret_Initialized = 1; |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | By default, hash randomization is disabled, and only |
| 360 | enabled if PYTHONHASHSEED is set to non-empty or if |
| 361 | "-R" is provided at the command line: |
| 362 | */ |
| 363 | if (!Py_HashRandomizationFlag) { |
| 364 | /* Disable the randomized hash: */ |
| 365 | memset(secret, 0, secret_size); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | Hash randomization is enabled. Generate a per-process secret, |
| 371 | using PYTHONHASHSEED if provided. |
| 372 | */ |
| 373 | |
| 374 | env = Py_GETENV("PYTHONHASHSEED"); |
| 375 | if (env && *env != '\0' && strcmp(env, "random") != 0) { |
| 376 | char *endptr = env; |
| 377 | unsigned long seed; |
| 378 | seed = strtoul(env, &endptr, 10); |
| 379 | if (*endptr != '\0' |
| 380 | || seed > 4294967295UL |
| 381 | || (errno == ERANGE && seed == ULONG_MAX)) |
| 382 | { |
| 383 | Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer " |
| 384 | "in range [0; 4294967295]"); |
| 385 | } |
| 386 | if (seed == 0) { |
| 387 | /* disable the randomized hash */ |
| 388 | memset(secret, 0, secret_size); |
| 389 | } |
| 390 | else { |
| 391 | lcg_urandom(seed, (unsigned char*)secret, secret_size); |
| 392 | } |
| 393 | } |
| 394 | else { |
| 395 | #ifdef MS_WINDOWS |
| 396 | (void)win32_urandom((unsigned char *)secret, secret_size, 0); |
Benjamin Peterson | 27c269a | 2014-12-26 11:09:00 -0600 | [diff] [blame] | 397 | #elif __VMS |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 398 | vms_urandom((unsigned char *)secret, secret_size, 0); |
Benjamin Peterson | 27c269a | 2014-12-26 11:09:00 -0600 | [diff] [blame] | 399 | #elif HAVE_GETENTROPY |
| 400 | (void)py_getentropy(secret, secret_size, 1); |
| 401 | #else |
| 402 | dev_urandom_noraise(secret, secret_size); |
Barry Warsaw | 1e13eb0 | 2012-02-20 20:42:21 -0500 | [diff] [blame] | 403 | #endif |
| 404 | } |
| 405 | } |
Benjamin Peterson | 57057a6 | 2014-08-28 12:30:00 -0400 | [diff] [blame] | 406 | |
| 407 | void |
| 408 | _PyRandom_Fini(void) |
| 409 | { |
| 410 | #ifdef MS_WINDOWS |
| 411 | if (hCryptProv) { |
| 412 | CryptReleaseContext(hCryptProv, 0); |
| 413 | hCryptProv = 0; |
| 414 | } |
Benjamin Peterson | 27c269a | 2014-12-26 11:09:00 -0600 | [diff] [blame] | 415 | #elif HAVE_GETENTROPY |
| 416 | /* nothing to clean */ |
Benjamin Peterson | 57057a6 | 2014-08-28 12:30:00 -0400 | [diff] [blame] | 417 | #else |
| 418 | dev_urandom_close(); |
| 419 | #endif |
| 420 | } |