blob: d10dda9eca22c51babb02be82bb884aea38f6ea5 [file] [log] [blame]
Barry Warsaw1e13eb02012-02-20 20:42:21 -05001#include "Python.h"
2#ifdef MS_WINDOWS
3#include <windows.h>
4#else
5#include <fcntl.h>
6#endif
7
Benjamin Peterson26da9202012-02-21 11:08:50 -05008#ifdef Py_DEBUG
9int _Py_HashSecret_Initialized = 0;
10#else
11static int _Py_HashSecret_Initialized = 0;
12#endif
Barry Warsaw1e13eb02012-02-20 20:42:21 -050013
14#ifdef MS_WINDOWS
15typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
16 LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType,\
17 DWORD dwFlags );
18typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
19 BYTE *pbBuffer );
20
21static CRYPTGENRANDOM pCryptGenRandom = NULL;
22/* This handle is never explicitly released. Instead, the operating
23 system will release it when the process terminates. */
24static HCRYPTPROV hCryptProv = 0;
25
26static int
27win32_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
56error:
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. */
66static int
67win32_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 Warsaw1e13eb02012-02-20 20:42:21 -050095
Benjamin Peterson27c269a2014-12-26 11:09:00 -060096#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. */
101static int
102py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
103{
104 while (size > 0) {
105 Py_ssize_t len = Py_MIN(size, 256);
106 int res = getentropy(buffer, len);
107 if (res < 0) {
108 if (fatal) {
109 Py_FatalError("getentropy() failed");
110 }
111 else {
112 PyErr_SetFromErrno(PyExc_OSError);
113 return -1;
114 }
115 }
116 buffer += len;
117 size -= len;
118 }
119 return 0;
120}
121#endif
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500122
123#ifdef __VMS
124/* Use openssl random routine */
125#include <openssl/rand.h>
126static int
127vms_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
128{
129 if (RAND_pseudo_bytes(buffer, size) < 0) {
130 if (raise) {
131 PyErr_Format(PyExc_ValueError,
132 "RAND_pseudo_bytes");
133 } else {
134 Py_FatalError("Failed to initialize the randomized hash "
135 "secret using RAND_pseudo_bytes");
136 }
137 return -1;
138 }
139 return 0;
140}
141#endif /* __VMS */
142
143
144#if !defined(MS_WINDOWS) && !defined(__VMS)
145
Benjamin Peterson57057a62014-08-28 12:30:00 -0400146static struct {
147 int fd;
148 dev_t st_dev;
149 ino_t st_ino;
150} urandom_cache = { -1 };
151
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500152/* Read size bytes from /dev/urandom into buffer.
153 Call Py_FatalError() on error. */
154static void
Benjamin Peterson57057a62014-08-28 12:30:00 -0400155dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size)
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500156{
157 int fd;
158 Py_ssize_t n;
159
160 assert (0 < size);
161
162 fd = open("/dev/urandom", O_RDONLY);
163 if (fd < 0)
164 Py_FatalError("Failed to open /dev/urandom");
165
166 while (0 < size)
167 {
168 do {
169 n = read(fd, buffer, (size_t)size);
170 } while (n < 0 && errno == EINTR);
171 if (n <= 0)
172 {
173 /* stop on error or if read(size) returned 0 */
174 Py_FatalError("Failed to read bytes from /dev/urandom");
175 break;
176 }
177 buffer += n;
178 size -= (Py_ssize_t)n;
179 }
180 close(fd);
181}
182
183/* Read size bytes from /dev/urandom into buffer.
184 Return 0 on success, raise an exception and return -1 on error. */
185static int
186dev_urandom_python(char *buffer, Py_ssize_t size)
187{
188 int fd;
189 Py_ssize_t n;
Benjamin Peterson57057a62014-08-28 12:30:00 -0400190 struct stat st;
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500191
192 if (size <= 0)
193 return 0;
194
Benjamin Peterson57057a62014-08-28 12:30:00 -0400195 if (urandom_cache.fd >= 0) {
196 /* Does the fd point to the same thing as before? (issue #21207) */
197 if (fstat(urandom_cache.fd, &st)
198 || st.st_dev != urandom_cache.st_dev
199 || st.st_ino != urandom_cache.st_ino) {
200 /* Something changed: forget the cached fd (but don't close it,
201 since it probably points to something important for some
202 third-party code). */
203 urandom_cache.fd = -1;
204 }
205 }
206 if (urandom_cache.fd >= 0)
207 fd = urandom_cache.fd;
208 else {
209 Py_BEGIN_ALLOW_THREADS
210 fd = open("/dev/urandom", O_RDONLY);
211 Py_END_ALLOW_THREADS
212 if (fd < 0)
213 {
214 if (errno == ENOENT || errno == ENXIO ||
215 errno == ENODEV || errno == EACCES)
216 PyErr_SetString(PyExc_NotImplementedError,
217 "/dev/urandom (or equivalent) not found");
218 else
219 PyErr_SetFromErrno(PyExc_OSError);
220 return -1;
221 }
222 if (urandom_cache.fd >= 0) {
223 /* urandom_fd was initialized by another thread while we were
224 not holding the GIL, keep it. */
225 close(fd);
226 fd = urandom_cache.fd;
227 }
228 else {
229 if (fstat(fd, &st)) {
230 PyErr_SetFromErrno(PyExc_OSError);
231 close(fd);
232 return -1;
233 }
234 else {
235 urandom_cache.fd = fd;
236 urandom_cache.st_dev = st.st_dev;
237 urandom_cache.st_ino = st.st_ino;
238 }
239 }
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500240 }
241
242 Py_BEGIN_ALLOW_THREADS
243 do {
244 do {
245 n = read(fd, buffer, (size_t)size);
246 } while (n < 0 && errno == EINTR);
247 if (n <= 0)
248 break;
249 buffer += n;
250 size -= (Py_ssize_t)n;
251 } while (0 < size);
252 Py_END_ALLOW_THREADS
253
254 if (n <= 0)
255 {
256 /* stop on error or if read(size) returned 0 */
257 if (n < 0)
258 PyErr_SetFromErrno(PyExc_OSError);
259 else
260 PyErr_Format(PyExc_RuntimeError,
261 "Failed to read %zi bytes from /dev/urandom",
262 size);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500263 return -1;
264 }
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500265 return 0;
266}
Benjamin Peterson57057a62014-08-28 12:30:00 -0400267
268static void
269dev_urandom_close(void)
270{
271 if (urandom_cache.fd >= 0) {
272 close(urandom_cache.fd);
273 urandom_cache.fd = -1;
274 }
275}
276
277
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500278#endif /* !defined(MS_WINDOWS) && !defined(__VMS) */
279
280/* Fill buffer with pseudo-random bytes generated by a linear congruent
281 generator (LCG):
282
283 x(n+1) = (x(n) * 214013 + 2531011) % 2^32
284
285 Use bits 23..16 of x(n) to generate a byte. */
286static void
287lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size)
288{
289 size_t index;
290 unsigned int x;
291
292 x = x0;
293 for (index=0; index < size; index++) {
294 x *= 214013;
295 x += 2531011;
296 /* modulo 2 ^ (8 * sizeof(int)) */
297 buffer[index] = (x >> 16) & 0xff;
298 }
299}
300
Georg Brandlc0edade2013-10-06 18:43:19 +0200301/* Fill buffer with size pseudo-random bytes from the operating system random
Serhiy Storchaka0f8f7842014-12-01 18:16:30 +0200302 number generator (RNG). It is suitable for most cryptographic purposes
Georg Brandlc0edade2013-10-06 18:43:19 +0200303 except long living private keys for asymmetric encryption.
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500304
305 Return 0 on success, raise an exception and return -1 on error. */
306int
307_PyOS_URandom(void *buffer, Py_ssize_t size)
308{
309 if (size < 0) {
310 PyErr_Format(PyExc_ValueError,
311 "negative argument not allowed");
312 return -1;
313 }
314 if (size == 0)
315 return 0;
316
317#ifdef MS_WINDOWS
318 return win32_urandom((unsigned char *)buffer, size, 1);
Benjamin Peterson27c269a2014-12-26 11:09:00 -0600319#elif HAVE_GETENTROPY
320 return py_getentropy(buffer, size, 0);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500321#else
322# ifdef __VMS
323 return vms_urandom((unsigned char *)buffer, size, 1);
324# else
325 return dev_urandom_python((char*)buffer, size);
326# endif
327#endif
328}
329
330void
331_PyRandom_Init(void)
332{
333 char *env;
334 void *secret = &_Py_HashSecret;
Benjamin Peterson26da9202012-02-21 11:08:50 -0500335 Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500336
Benjamin Peterson26da9202012-02-21 11:08:50 -0500337 if (_Py_HashSecret_Initialized)
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500338 return;
Benjamin Peterson26da9202012-02-21 11:08:50 -0500339 _Py_HashSecret_Initialized = 1;
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500340
341 /*
342 By default, hash randomization is disabled, and only
343 enabled if PYTHONHASHSEED is set to non-empty or if
344 "-R" is provided at the command line:
345 */
346 if (!Py_HashRandomizationFlag) {
347 /* Disable the randomized hash: */
348 memset(secret, 0, secret_size);
349 return;
350 }
351
352 /*
353 Hash randomization is enabled. Generate a per-process secret,
354 using PYTHONHASHSEED if provided.
355 */
356
357 env = Py_GETENV("PYTHONHASHSEED");
358 if (env && *env != '\0' && strcmp(env, "random") != 0) {
359 char *endptr = env;
360 unsigned long seed;
361 seed = strtoul(env, &endptr, 10);
362 if (*endptr != '\0'
363 || seed > 4294967295UL
364 || (errno == ERANGE && seed == ULONG_MAX))
365 {
366 Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
367 "in range [0; 4294967295]");
368 }
369 if (seed == 0) {
370 /* disable the randomized hash */
371 memset(secret, 0, secret_size);
372 }
373 else {
374 lcg_urandom(seed, (unsigned char*)secret, secret_size);
375 }
376 }
377 else {
378#ifdef MS_WINDOWS
379 (void)win32_urandom((unsigned char *)secret, secret_size, 0);
Benjamin Peterson27c269a2014-12-26 11:09:00 -0600380#elif __VMS
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500381 vms_urandom((unsigned char *)secret, secret_size, 0);
Benjamin Peterson27c269a2014-12-26 11:09:00 -0600382#elif HAVE_GETENTROPY
383 (void)py_getentropy(secret, secret_size, 1);
384#else
385 dev_urandom_noraise(secret, secret_size);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500386#endif
387 }
388}
Benjamin Peterson57057a62014-08-28 12:30:00 -0400389
390void
391_PyRandom_Fini(void)
392{
393#ifdef MS_WINDOWS
394 if (hCryptProv) {
395 CryptReleaseContext(hCryptProv, 0);
396 hCryptProv = 0;
397 }
Benjamin Peterson27c269a2014-12-26 11:09:00 -0600398#elif HAVE_GETENTROPY
399 /* nothing to clean */
Benjamin Peterson57057a62014-08-28 12:30:00 -0400400#else
401 dev_urandom_close();
402#endif
403}