blob: de8e9e72c74c54e3b55af29ef8c724b1aa03e195 [file] [log] [blame]
Georg Brandl2daf6ae2012-02-20 19:54:16 +01001#include "Python.h"
2#ifdef MS_WINDOWS
3#include <windows.h>
4#else
5#include <fcntl.h>
6#endif
7
Benjamin Peterson69e97272012-02-21 11:08:50 -05008#ifdef Py_DEBUG
9int _Py_HashSecret_Initialized = 0;
10#else
11static int _Py_HashSecret_Initialized = 0;
12#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010013
14#ifdef MS_WINDOWS
Georg Brandl2daf6ae2012-02-20 19:54:16 +010015/* This handle is never explicitly released. Instead, the operating
16 system will release it when the process terminates. */
17static HCRYPTPROV hCryptProv = 0;
18
19static int
20win32_urandom_init(int raise)
21{
Georg Brandl2daf6ae2012-02-20 19:54:16 +010022 /* Acquire context */
Martin v. Löwis3f50bf62013-01-25 14:06:18 +010023 if (!CryptAcquireContext(&hCryptProv, NULL, NULL,
24 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010025 goto error;
26
27 return 0;
28
29error:
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. */
39static int
40win32_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 Stinner0c083462013-11-15 23:26:25 +010053 if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer))
Georg Brandl2daf6ae2012-02-20 19:54:16 +010054 {
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>
74static int
75vms_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 Pitrou4879a962013-08-31 00:26:02 +020093static int urandom_fd = -1;
Georg Brandl2daf6ae2012-02-20 19:54:16 +010094
95/* Read size bytes from /dev/urandom into buffer.
96 Call Py_FatalError() on error. */
97static void
Christian Heimes985ecdc2013-11-20 11:46:18 +010098dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size)
Georg Brandl2daf6ae2012-02-20 19:54:16 +010099{
100 int fd;
101 Py_ssize_t n;
102
103 assert (0 < size);
104
Victor Stinnerdaf45552013-08-28 00:53:59 +0200105 fd = _Py_open("/dev/urandom", O_RDONLY);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100106 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. */
128static int
129dev_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 Pitrou4879a962013-08-31 00:26:02 +0200137 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 Pitrouec34ab52013-08-16 20:44:38 +0200159 else
Antoine Pitrou4879a962013-08-31 00:26:02 +0200160 urandom_fd = fd;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100161 }
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 Brandl2daf6ae2012-02-20 19:54:16 +0100184 return -1;
185 }
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100186 return 0;
187}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200188
189static void
190dev_urandom_close(void)
191{
192 if (urandom_fd >= 0) {
193 close(urandom_fd);
194 urandom_fd = -1;
195 }
196}
197
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100198#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. */
206static void
207lcg_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 Brandlc6a2c9b2013-10-06 18:43:19 +0200221/* 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 Brandl2daf6ae2012-02-20 19:54:16 +0100224
225 Return 0 on success, raise an exception and return -1 on error. */
226int
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
248void
249_PyRandom_Init(void)
250{
251 char *env;
Christian Heimes985ecdc2013-11-20 11:46:18 +0100252 unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500253 Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
Christian Heimes985ecdc2013-11-20 11:46:18 +0100254 assert(secret_size == sizeof(_Py_HashSecret.uc));
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100255
Benjamin Peterson69e97272012-02-21 11:08:50 -0500256 if (_Py_HashSecret_Initialized)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100257 return;
Benjamin Peterson69e97272012-02-21 11:08:50 -0500258 _Py_HashSecret_Initialized = 1;
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100259
260 /*
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100261 Hash randomization is enabled. Generate a per-process secret,
262 using PYTHONHASHSEED if provided.
263 */
264
265 env = Py_GETENV("PYTHONHASHSEED");
Georg Brandl12897d72012-02-20 23:49:29 +0100266 if (env && *env != '\0' && strcmp(env, "random") != 0) {
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100267 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 Heimes985ecdc2013-11-20 11:46:18 +0100282 lcg_urandom(seed, secret, secret_size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100283 }
284 }
285 else {
286#ifdef MS_WINDOWS
Christian Heimes985ecdc2013-11-20 11:46:18 +0100287 (void)win32_urandom(secret, secret_size, 0);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100288#else /* #ifdef MS_WINDOWS */
289# ifdef __VMS
Christian Heimes985ecdc2013-11-20 11:46:18 +0100290 vms_urandom(secret, secret_size, 0);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100291# else
Christian Heimes985ecdc2013-11-20 11:46:18 +0100292 dev_urandom_noraise(secret, secret_size);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100293# endif
294#endif
295 }
296}
Antoine Pitrou4879a962013-08-31 00:26:02 +0200297
298void
299_PyRandom_Fini(void)
300{
301#if !defined(MS_WINDOWS) && !defined(__VMS)
302 dev_urandom_close();
303#endif
304}