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 | |
| 71 | #ifdef __VMS |
| 72 | /* Use openssl random routine */ |
| 73 | #include <openssl/rand.h> |
| 74 | static int |
| 75 | vms_urandom(unsigned char *buffer, Py_ssize_t size, int raise) |
| 76 | { |
| 77 | if (RAND_pseudo_bytes(buffer, size) < 0) { |
| 78 | if (raise) { |
| 79 | PyErr_Format(PyExc_ValueError, |
| 80 | "RAND_pseudo_bytes"); |
| 81 | } else { |
| 82 | Py_FatalError("Failed to initialize the randomized hash " |
| 83 | "secret using RAND_pseudo_bytes"); |
| 84 | } |
| 85 | return -1; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | #endif /* __VMS */ |
| 90 | |
| 91 | |
| 92 | #if !defined(MS_WINDOWS) && !defined(__VMS) |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 93 | static int urandom_fd = -1; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 94 | |
| 95 | /* Read size bytes from /dev/urandom into buffer. |
| 96 | Call Py_FatalError() on error. */ |
| 97 | static void |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 98 | dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 99 | { |
| 100 | int fd; |
| 101 | Py_ssize_t n; |
| 102 | |
| 103 | assert (0 < size); |
| 104 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 105 | fd = _Py_open("/dev/urandom", O_RDONLY); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 106 | if (fd < 0) |
| 107 | Py_FatalError("Failed to open /dev/urandom"); |
| 108 | |
| 109 | while (0 < size) |
| 110 | { |
| 111 | do { |
| 112 | n = read(fd, buffer, (size_t)size); |
| 113 | } while (n < 0 && errno == EINTR); |
| 114 | if (n <= 0) |
| 115 | { |
| 116 | /* stop on error or if read(size) returned 0 */ |
| 117 | Py_FatalError("Failed to read bytes from /dev/urandom"); |
| 118 | break; |
| 119 | } |
| 120 | buffer += n; |
| 121 | size -= (Py_ssize_t)n; |
| 122 | } |
| 123 | close(fd); |
| 124 | } |
| 125 | |
| 126 | /* Read size bytes from /dev/urandom into buffer. |
| 127 | Return 0 on success, raise an exception and return -1 on error. */ |
| 128 | static int |
| 129 | dev_urandom_python(char *buffer, Py_ssize_t size) |
| 130 | { |
| 131 | int fd; |
| 132 | Py_ssize_t n; |
| 133 | |
| 134 | if (size <= 0) |
| 135 | return 0; |
| 136 | |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 137 | if (urandom_fd >= 0) |
| 138 | fd = urandom_fd; |
| 139 | else { |
| 140 | Py_BEGIN_ALLOW_THREADS |
| 141 | fd = _Py_open("/dev/urandom", O_RDONLY); |
| 142 | Py_END_ALLOW_THREADS |
| 143 | if (fd < 0) |
| 144 | { |
| 145 | if (errno == ENOENT || errno == ENXIO || |
| 146 | errno == ENODEV || errno == EACCES) |
| 147 | PyErr_SetString(PyExc_NotImplementedError, |
| 148 | "/dev/urandom (or equivalent) not found"); |
| 149 | else |
| 150 | PyErr_SetFromErrno(PyExc_OSError); |
| 151 | return -1; |
| 152 | } |
| 153 | if (urandom_fd >= 0) { |
| 154 | /* urandom_fd was initialized by another thread while we were |
| 155 | not holding the GIL, keep it. */ |
| 156 | close(fd); |
| 157 | fd = urandom_fd; |
| 158 | } |
Antoine Pitrou | ec34ab5 | 2013-08-16 20:44:38 +0200 | [diff] [blame] | 159 | else |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 160 | urandom_fd = fd; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | Py_BEGIN_ALLOW_THREADS |
| 164 | do { |
| 165 | do { |
| 166 | n = read(fd, buffer, (size_t)size); |
| 167 | } while (n < 0 && errno == EINTR); |
| 168 | if (n <= 0) |
| 169 | break; |
| 170 | buffer += n; |
| 171 | size -= (Py_ssize_t)n; |
| 172 | } while (0 < size); |
| 173 | Py_END_ALLOW_THREADS |
| 174 | |
| 175 | if (n <= 0) |
| 176 | { |
| 177 | /* stop on error or if read(size) returned 0 */ |
| 178 | if (n < 0) |
| 179 | PyErr_SetFromErrno(PyExc_OSError); |
| 180 | else |
| 181 | PyErr_Format(PyExc_RuntimeError, |
| 182 | "Failed to read %zi bytes from /dev/urandom", |
| 183 | size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 184 | return -1; |
| 185 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 186 | return 0; |
| 187 | } |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 188 | |
| 189 | static void |
| 190 | dev_urandom_close(void) |
| 191 | { |
| 192 | if (urandom_fd >= 0) { |
| 193 | close(urandom_fd); |
| 194 | urandom_fd = -1; |
| 195 | } |
| 196 | } |
| 197 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 198 | #endif /* !defined(MS_WINDOWS) && !defined(__VMS) */ |
| 199 | |
| 200 | /* Fill buffer with pseudo-random bytes generated by a linear congruent |
| 201 | generator (LCG): |
| 202 | |
| 203 | x(n+1) = (x(n) * 214013 + 2531011) % 2^32 |
| 204 | |
| 205 | Use bits 23..16 of x(n) to generate a byte. */ |
| 206 | static void |
| 207 | lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size) |
| 208 | { |
| 209 | size_t index; |
| 210 | unsigned int x; |
| 211 | |
| 212 | x = x0; |
| 213 | for (index=0; index < size; index++) { |
| 214 | x *= 214013; |
| 215 | x += 2531011; |
| 216 | /* modulo 2 ^ (8 * sizeof(int)) */ |
| 217 | buffer[index] = (x >> 16) & 0xff; |
| 218 | } |
| 219 | } |
| 220 | |
Georg Brandl | c6a2c9b | 2013-10-06 18:43:19 +0200 | [diff] [blame] | 221 | /* Fill buffer with size pseudo-random bytes from the operating system random |
| 222 | number generator (RNG). It is suitable for for most cryptographic purposes |
| 223 | except long living private keys for asymmetric encryption. |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 224 | |
| 225 | Return 0 on success, raise an exception and return -1 on error. */ |
| 226 | int |
| 227 | _PyOS_URandom(void *buffer, Py_ssize_t size) |
| 228 | { |
| 229 | if (size < 0) { |
| 230 | PyErr_Format(PyExc_ValueError, |
| 231 | "negative argument not allowed"); |
| 232 | return -1; |
| 233 | } |
| 234 | if (size == 0) |
| 235 | return 0; |
| 236 | |
| 237 | #ifdef MS_WINDOWS |
| 238 | return win32_urandom((unsigned char *)buffer, size, 1); |
| 239 | #else |
| 240 | # ifdef __VMS |
| 241 | return vms_urandom((unsigned char *)buffer, size, 1); |
| 242 | # else |
| 243 | return dev_urandom_python((char*)buffer, size); |
| 244 | # endif |
| 245 | #endif |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | _PyRandom_Init(void) |
| 250 | { |
| 251 | char *env; |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 252 | unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc; |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 253 | Py_ssize_t secret_size = sizeof(_Py_HashSecret_t); |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 254 | assert(secret_size == sizeof(_Py_HashSecret.uc)); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 255 | |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 256 | if (_Py_HashSecret_Initialized) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 257 | return; |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 258 | _Py_HashSecret_Initialized = 1; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 259 | |
| 260 | /* |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 261 | Hash randomization is enabled. Generate a per-process secret, |
| 262 | using PYTHONHASHSEED if provided. |
| 263 | */ |
| 264 | |
| 265 | env = Py_GETENV("PYTHONHASHSEED"); |
Georg Brandl | 12897d7 | 2012-02-20 23:49:29 +0100 | [diff] [blame] | 266 | if (env && *env != '\0' && strcmp(env, "random") != 0) { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 267 | char *endptr = env; |
| 268 | unsigned long seed; |
| 269 | seed = strtoul(env, &endptr, 10); |
| 270 | if (*endptr != '\0' |
| 271 | || seed > 4294967295UL |
| 272 | || (errno == ERANGE && seed == ULONG_MAX)) |
| 273 | { |
| 274 | Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer " |
| 275 | "in range [0; 4294967295]"); |
| 276 | } |
| 277 | if (seed == 0) { |
| 278 | /* disable the randomized hash */ |
| 279 | memset(secret, 0, secret_size); |
| 280 | } |
| 281 | else { |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 282 | lcg_urandom(seed, secret, secret_size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | else { |
| 286 | #ifdef MS_WINDOWS |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 287 | (void)win32_urandom(secret, secret_size, 0); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 288 | #else /* #ifdef MS_WINDOWS */ |
| 289 | # ifdef __VMS |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 290 | vms_urandom(secret, secret_size, 0); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 291 | # else |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 292 | dev_urandom_noraise(secret, secret_size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 293 | # endif |
| 294 | #endif |
| 295 | } |
| 296 | } |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 297 | |
| 298 | void |
| 299 | _PyRandom_Fini(void) |
| 300 | { |
| 301 | #if !defined(MS_WINDOWS) && !defined(__VMS) |
| 302 | dev_urandom_close(); |
| 303 | #endif |
| 304 | } |