blob: 1e5776d6183e9b2360a83b2ce0c155f9211d977c [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}
95#endif /* MS_WINDOWS */
96
97
98#ifdef __VMS
99/* Use openssl random routine */
100#include <openssl/rand.h>
101static int
102vms_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
103{
104 if (RAND_pseudo_bytes(buffer, size) < 0) {
105 if (raise) {
106 PyErr_Format(PyExc_ValueError,
107 "RAND_pseudo_bytes");
108 } else {
109 Py_FatalError("Failed to initialize the randomized hash "
110 "secret using RAND_pseudo_bytes");
111 }
112 return -1;
113 }
114 return 0;
115}
116#endif /* __VMS */
117
118
119#if !defined(MS_WINDOWS) && !defined(__VMS)
120
Benjamin Peterson57057a62014-08-28 12:30:00 -0400121static struct {
122 int fd;
123 dev_t st_dev;
124 ino_t st_ino;
125} urandom_cache = { -1 };
126
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500127/* Read size bytes from /dev/urandom into buffer.
128 Call Py_FatalError() on error. */
129static void
Benjamin Peterson57057a62014-08-28 12:30:00 -0400130dev_urandom_noraise(unsigned char *buffer, Py_ssize_t size)
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500131{
132 int fd;
133 Py_ssize_t n;
134
135 assert (0 < size);
136
137 fd = open("/dev/urandom", O_RDONLY);
138 if (fd < 0)
139 Py_FatalError("Failed to open /dev/urandom");
140
141 while (0 < size)
142 {
143 do {
144 n = read(fd, buffer, (size_t)size);
145 } while (n < 0 && errno == EINTR);
146 if (n <= 0)
147 {
148 /* stop on error or if read(size) returned 0 */
149 Py_FatalError("Failed to read bytes from /dev/urandom");
150 break;
151 }
152 buffer += n;
153 size -= (Py_ssize_t)n;
154 }
155 close(fd);
156}
157
158/* Read size bytes from /dev/urandom into buffer.
159 Return 0 on success, raise an exception and return -1 on error. */
160static int
161dev_urandom_python(char *buffer, Py_ssize_t size)
162{
163 int fd;
164 Py_ssize_t n;
Benjamin Peterson57057a62014-08-28 12:30:00 -0400165 struct stat st;
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500166
167 if (size <= 0)
168 return 0;
169
Benjamin Peterson57057a62014-08-28 12:30:00 -0400170 if (urandom_cache.fd >= 0) {
171 /* Does the fd point to the same thing as before? (issue #21207) */
172 if (fstat(urandom_cache.fd, &st)
173 || st.st_dev != urandom_cache.st_dev
174 || st.st_ino != urandom_cache.st_ino) {
175 /* Something changed: forget the cached fd (but don't close it,
176 since it probably points to something important for some
177 third-party code). */
178 urandom_cache.fd = -1;
179 }
180 }
181 if (urandom_cache.fd >= 0)
182 fd = urandom_cache.fd;
183 else {
184 Py_BEGIN_ALLOW_THREADS
185 fd = open("/dev/urandom", O_RDONLY);
186 Py_END_ALLOW_THREADS
187 if (fd < 0)
188 {
189 if (errno == ENOENT || errno == ENXIO ||
190 errno == ENODEV || errno == EACCES)
191 PyErr_SetString(PyExc_NotImplementedError,
192 "/dev/urandom (or equivalent) not found");
193 else
194 PyErr_SetFromErrno(PyExc_OSError);
195 return -1;
196 }
197 if (urandom_cache.fd >= 0) {
198 /* urandom_fd was initialized by another thread while we were
199 not holding the GIL, keep it. */
200 close(fd);
201 fd = urandom_cache.fd;
202 }
203 else {
204 if (fstat(fd, &st)) {
205 PyErr_SetFromErrno(PyExc_OSError);
206 close(fd);
207 return -1;
208 }
209 else {
210 urandom_cache.fd = fd;
211 urandom_cache.st_dev = st.st_dev;
212 urandom_cache.st_ino = st.st_ino;
213 }
214 }
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500215 }
216
217 Py_BEGIN_ALLOW_THREADS
218 do {
219 do {
220 n = read(fd, buffer, (size_t)size);
221 } while (n < 0 && errno == EINTR);
222 if (n <= 0)
223 break;
224 buffer += n;
225 size -= (Py_ssize_t)n;
226 } while (0 < size);
227 Py_END_ALLOW_THREADS
228
229 if (n <= 0)
230 {
231 /* stop on error or if read(size) returned 0 */
232 if (n < 0)
233 PyErr_SetFromErrno(PyExc_OSError);
234 else
235 PyErr_Format(PyExc_RuntimeError,
236 "Failed to read %zi bytes from /dev/urandom",
237 size);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500238 return -1;
239 }
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500240 return 0;
241}
Benjamin Peterson57057a62014-08-28 12:30:00 -0400242
243static void
244dev_urandom_close(void)
245{
246 if (urandom_cache.fd >= 0) {
247 close(urandom_cache.fd);
248 urandom_cache.fd = -1;
249 }
250}
251
252
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500253#endif /* !defined(MS_WINDOWS) && !defined(__VMS) */
254
255/* Fill buffer with pseudo-random bytes generated by a linear congruent
256 generator (LCG):
257
258 x(n+1) = (x(n) * 214013 + 2531011) % 2^32
259
260 Use bits 23..16 of x(n) to generate a byte. */
261static void
262lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size)
263{
264 size_t index;
265 unsigned int x;
266
267 x = x0;
268 for (index=0; index < size; index++) {
269 x *= 214013;
270 x += 2531011;
271 /* modulo 2 ^ (8 * sizeof(int)) */
272 buffer[index] = (x >> 16) & 0xff;
273 }
274}
275
Georg Brandlc0edade2013-10-06 18:43:19 +0200276/* Fill buffer with size pseudo-random bytes from the operating system random
Serhiy Storchaka0f8f7842014-12-01 18:16:30 +0200277 number generator (RNG). It is suitable for most cryptographic purposes
Georg Brandlc0edade2013-10-06 18:43:19 +0200278 except long living private keys for asymmetric encryption.
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500279
280 Return 0 on success, raise an exception and return -1 on error. */
281int
282_PyOS_URandom(void *buffer, Py_ssize_t size)
283{
284 if (size < 0) {
285 PyErr_Format(PyExc_ValueError,
286 "negative argument not allowed");
287 return -1;
288 }
289 if (size == 0)
290 return 0;
291
292#ifdef MS_WINDOWS
293 return win32_urandom((unsigned char *)buffer, size, 1);
294#else
295# ifdef __VMS
296 return vms_urandom((unsigned char *)buffer, size, 1);
297# else
298 return dev_urandom_python((char*)buffer, size);
299# endif
300#endif
301}
302
303void
304_PyRandom_Init(void)
305{
306 char *env;
307 void *secret = &_Py_HashSecret;
Benjamin Peterson26da9202012-02-21 11:08:50 -0500308 Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500309
Benjamin Peterson26da9202012-02-21 11:08:50 -0500310 if (_Py_HashSecret_Initialized)
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500311 return;
Benjamin Peterson26da9202012-02-21 11:08:50 -0500312 _Py_HashSecret_Initialized = 1;
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500313
314 /*
315 By default, hash randomization is disabled, and only
316 enabled if PYTHONHASHSEED is set to non-empty or if
317 "-R" is provided at the command line:
318 */
319 if (!Py_HashRandomizationFlag) {
320 /* Disable the randomized hash: */
321 memset(secret, 0, secret_size);
322 return;
323 }
324
325 /*
326 Hash randomization is enabled. Generate a per-process secret,
327 using PYTHONHASHSEED if provided.
328 */
329
330 env = Py_GETENV("PYTHONHASHSEED");
331 if (env && *env != '\0' && strcmp(env, "random") != 0) {
332 char *endptr = env;
333 unsigned long seed;
334 seed = strtoul(env, &endptr, 10);
335 if (*endptr != '\0'
336 || seed > 4294967295UL
337 || (errno == ERANGE && seed == ULONG_MAX))
338 {
339 Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
340 "in range [0; 4294967295]");
341 }
342 if (seed == 0) {
343 /* disable the randomized hash */
344 memset(secret, 0, secret_size);
345 }
346 else {
347 lcg_urandom(seed, (unsigned char*)secret, secret_size);
348 }
349 }
350 else {
351#ifdef MS_WINDOWS
352 (void)win32_urandom((unsigned char *)secret, secret_size, 0);
353#else /* #ifdef MS_WINDOWS */
354# ifdef __VMS
355 vms_urandom((unsigned char *)secret, secret_size, 0);
356# else
Benjamin Peterson57057a62014-08-28 12:30:00 -0400357 dev_urandom_noraise((unsigned char*)secret, secret_size);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500358# endif
359#endif
360 }
361}
Benjamin Peterson57057a62014-08-28 12:30:00 -0400362
363void
364_PyRandom_Fini(void)
365{
366#ifdef MS_WINDOWS
367 if (hCryptProv) {
368 CryptReleaseContext(hCryptProv, 0);
369 hCryptProv = 0;
370 }
371#else
372 dev_urandom_close();
373#endif
374}