5 * Mark Crichton (crichton@gimp.org)
6 * Patrik Torstensson (p@rxc.se)
7 * Sebastien Pouliot (sebastien@ximian.com)
8 * Ludovic Henry (ludovic.henry@xamarin.com)
10 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
11 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 * Copyright 2001 Xamarin, Inc (http://www.novell.com)
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
21 #include "mono-error.h"
22 #include "mono-error-internals.h"
23 #include "mono-rand.h"
24 #include "mono-threads.h"
25 #include "metadata/exception.h"
26 #include "metadata/object.h"
33 #ifndef PROV_INTEL_SEC
34 #define PROV_INTEL_SEC 22
36 #ifndef CRYPT_VERIFY_CONTEXT
37 #define CRYPT_VERIFY_CONTEXT 0xF0000000
43 * Returns: True if random source is global, false if mono_rand_init can be called repeatedly to get randomness instances.
45 * Initializes entire RNG system. Must be called once per process before calling mono_rand_init.
55 * @seed: A string containing seed data
56 * @seed_size: Length of seed string
58 * Returns: On success, a non-NULL handle which can be used to fetch random data from mono_rand_try_get_bytes. On failure, NULL.
60 * Initializes an RNG client.
63 mono_rand_init (guchar *seed, gint seed_size)
65 HCRYPTPROV provider = 0;
67 /* There is no need to create a container for just random data,
68 * so we can use CRYPT_VERIFY_CONTEXT (one call) see:
69 * http://blogs.msdn.com/dangriff/archive/2003/11/19/51709.aspx */
71 /* We first try to use the Intel PIII RNG if drivers are present */
72 if (!CryptAcquireContext (&provider, NULL, NULL, PROV_INTEL_SEC, CRYPT_VERIFY_CONTEXT)) {
73 /* not a PIII or no drivers available, use default RSA CSP */
74 if (!CryptAcquireContext (&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFY_CONTEXT)) {
75 /* exception will be thrown in managed code */
80 /* seed the CSP with the supplied buffer (if present) */
81 if (provider != 0 && seed) {
82 /* the call we replace the seed with random - this isn't what is
83 * expected from the class library user */
84 guchar *data = g_malloc (seed_size);
86 memcpy (data, seed, seed_size);
87 /* add seeding material to the RNG */
88 CryptGenRandom (provider, seed_size, data);
89 /* zeroize and free */
90 memset (data, 0, seed_size);
95 return (gpointer) provider;
99 * mono_rand_try_get_bytes:
100 * @handle: A pointer to an RNG handle. Handle is set to NULL on failure.
101 * @buffer: A buffer into which to write random data.
102 * @buffer_size: Number of bytes to write into buffer.
103 * @error: Set on error.
105 * Returns: FALSE on failure and sets @error, TRUE on success.
107 * Extracts bytes from an RNG handle.
110 mono_rand_try_get_bytes (gpointer *handle, guchar *buffer, gint buffer_size, MonoError *error)
114 mono_error_init (error);
117 provider = (HCRYPTPROV) *handle;
119 if (!CryptGenRandom (provider, buffer_size, buffer)) {
120 CryptReleaseContext (provider, 0);
121 /* we may have lost our context with CryptoAPI, but all hope isn't lost yet! */
122 provider = (HCRYPTPROV) mono_rand_init (NULL, 0);
123 if (!CryptGenRandom (provider, buffer_size, buffer)) {
124 /* exception will be thrown in managed code */
125 CryptReleaseContext (provider, 0);
127 mono_error_set_execution_engine (error, "Failed to gen random bytes (%d)", GetLastError ());
136 * @handle: An RNG handle.
137 * @buffer: A buffer into which to write random data.
138 * @buffer_size: Number of bytes to write into buffer.
140 * Releases an RNG handle.
143 mono_rand_close (gpointer handle)
145 CryptReleaseContext ((HCRYPTPROV) handle, 0);
148 #elif defined (HAVE_SYS_UN_H) && !defined(__native_client__)
153 #include <sys/socket.h>
154 #include <sys/types.h>
157 #ifndef NAME_DEV_URANDOM
158 #define NAME_DEV_URANDOM "/dev/urandom"
161 static gboolean use_egd = FALSE;
162 static gint file = -1;
165 get_entropy_from_egd (const char *path, guchar *buffer, int buffer_size, MonoError *error)
167 struct sockaddr_un egd_addr;
173 mono_error_init (error);
175 socket_fd = socket (PF_UNIX, SOCK_STREAM, 0);
180 egd_addr.sun_family = AF_UNIX;
181 strncpy (egd_addr.sun_path, path, sizeof (egd_addr.sun_path) - 1);
182 egd_addr.sun_path [sizeof (egd_addr.sun_path) - 1] = '\0';
183 ret = connect (socket_fd, (struct sockaddr*) &egd_addr, sizeof (egd_addr));
189 g_warning ("Entropy problem! Can't create or connect to egd socket %s", path);
190 mono_error_set_execution_engine (error, "Failed to open egd socket %s: %s", path, strerror (err));
194 while (buffer_size > 0) {
198 /* block until daemon can return enough entropy */
200 request [1] = buffer_size < 255 ? buffer_size : 255;
202 int sent = write (socket_fd, request + count, 2 - count);
206 } else if (err == EINTR) {
210 g_warning ("Send egd request failed %d", err);
211 mono_error_set_execution_engine (error, "Failed to send request to egd socket: %s", strerror (err));
217 while (count != request [1]) {
219 received = read (socket_fd, buffer + offset, request [1] - count);
224 } else if (received < 0 && err == EINTR) {
228 g_warning ("Receive egd request failed %d", err);
229 mono_error_set_execution_engine (error, "Failed to get response from egd socket: %s", strerror(err));
234 buffer_size -= request [1];
241 mono_rand_open (void)
243 static gint32 status = 0;
244 if (status != 0 || InterlockedCompareExchange (&status, 1, 0) != 0) {
246 mono_thread_info_yield ();
250 #ifdef NAME_DEV_URANDOM
251 file = open (NAME_DEV_URANDOM, O_RDONLY);
253 #ifdef NAME_DEV_RANDOM
255 file = open (NAME_DEV_RANDOM, O_RDONLY);
258 use_egd = g_getenv("MONO_EGD_SOCKET") != NULL;
266 mono_rand_init (guchar *seed, gint seed_size)
268 // file < 0 is expected in the egd case
269 return (!use_egd && file < 0) ? NULL : GINT_TO_POINTER (file);
273 mono_rand_try_get_bytes (gpointer *handle, guchar *buffer, gint buffer_size, MonoError *error)
277 mono_error_init (error);
280 const char *socket_path = g_getenv ("MONO_EGD_SOCKET");
281 /* exception will be thrown in managed code */
282 if (socket_path == NULL) {
286 get_entropy_from_egd (socket_path, buffer, buffer_size, error);
288 /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */
293 err = read (file, buffer + count, buffer_size - count);
297 g_warning("Entropy error! Error in read (%s).", strerror (errno));
298 /* exception will be thrown in managed code */
299 mono_error_set_execution_engine (error, "Entropy error! Error in read (%s).", strerror (errno));
303 } while (count < buffer_size);
309 mono_rand_close (gpointer provider)
319 mono_rand_open (void)
321 static gint32 status = 0;
322 if (status != 0 || InterlockedCompareExchange (&status, 1, 0) != 0) {
324 mono_thread_info_yield ();
336 mono_rand_init (guchar *seed, gint seed_size)
338 return "srand"; // NULL will be interpreted as failure; return arbitrary nonzero pointer
342 mono_rand_try_get_bytes (gpointer *handle, guchar *buffer, gint buffer_size, MonoError *error)
346 mono_error_init (error);
349 if (buffer_size - count >= sizeof (gint32) && RAND_MAX >= 0xFFFFFFFF) {
350 *(gint32*) buffer = rand();
351 count += sizeof (gint32);
352 buffer += sizeof (gint32) / sizeof (guchar);
353 } else if (buffer_size - count >= sizeof (gint16) && RAND_MAX >= 0xFFFF) {
354 *(gint16*) buffer = rand();
355 count += sizeof (gint16);
356 buffer += sizeof (gint16) / sizeof (guchar);
357 } else if (buffer_size - count >= sizeof (gint8) && RAND_MAX >= 0xFF) {
358 *(gint8*) buffer = rand();
359 count += sizeof (gint8);
360 buffer += sizeof (gint8) / sizeof (guchar);
362 } while (count < buffer_size);
368 mono_rand_close (gpointer provider)
375 * mono_rand_try_get_uint32:
376 * @handle: A pointer to an RNG handle. Handle is set to NULL on failure.
377 * @val: A pointer to a 32-bit unsigned int, to which the result will be written.
378 * @min: Result will be greater than or equal to this value.
379 * @max: Result will be less than or equal to this value.
381 * Returns: FALSE on failure, TRUE on success.
383 * Extracts one 32-bit unsigned int from an RNG handle.
386 mono_rand_try_get_uint32 (gpointer *handle, guint32 *val, guint32 min, guint32 max, MonoError *error)
389 if (!mono_rand_try_get_bytes (handle, (guchar*) val, sizeof (guint32), error))
392 double randomDouble = ((gdouble) *val) / ( ((double)G_MAXUINT32) + 1 ); // Range is [0,1)
393 *val = (guint32) (randomDouble * (max - min + 1) + min);
395 g_assert (*val >= min);
396 g_assert (*val <= max);