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