2 * rand.c: System.Security.Cryptography.RNGCryptoServiceProvider support
5 * Mark Crichton (crichton@gimp.org)
6 * Patrik Torstensson (p@rxc.se)
7 * Sebastien Pouliot (sebastien@ximian.com)
9 * (C) 2001 Ximian, Inc.
10 * Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
15 #include <sys/types.h>
22 #include <mono/metadata/object.h>
23 #include <mono/metadata/rand.h>
24 #include <mono/metadata/exception.h>
26 #if !defined(PLATFORM_WIN32)
27 #include <sys/socket.h>
32 get_entropy_from_server (const char *path, guchar *buf, int len)
37 struct sockaddr_un egd_addr;
39 file = socket (PF_UNIX, SOCK_STREAM, 0);
43 egd_addr.sun_family = AF_UNIX;
44 strncpy (egd_addr.sun_path, path, MONO_SIZEOF_SUNPATH - 1);
45 egd_addr.sun_path [MONO_SIZEOF_SUNPATH-1] = '\0';
46 ret = connect (file, (struct sockaddr *)&egd_addr, sizeof(egd_addr));
51 g_warning ("Entropy problem! Can't create or connect to egd socket %s", path);
52 mono_raise_exception (mono_get_exception_execution_engine ("Failed to open egd socket"));
59 request [0] = 2; /* block until daemon can return enough entropy */
60 request [1] = len < 255 ? len : 255;
62 int sent = write (file, request + count, 2 - count);
65 else if (errno == EINTR)
69 g_warning ("Send egd request failed %d", errno);
70 mono_raise_exception (mono_get_exception_execution_engine ("Failed to send request to egd socket"));
75 while (count != request [1]) {
77 received = read(file, buf + offset, request [1] - count);
81 } else if (received < 0 && errno == EINTR) {
85 g_warning ("Receive egd request failed %d", errno);
86 mono_raise_exception (mono_get_exception_execution_engine ("Failed to get response from egd socket"));
97 #if defined (PLATFORM_WIN32)
100 #include <wincrypt.h>
102 #ifndef PROV_INTEL_SEC
103 #define PROV_INTEL_SEC 22
105 #ifndef CRYPT_VERIFY_CONTEXT
106 #define CRYPT_VERIFY_CONTEXT 0xF0000000
110 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngOpen (void)
112 /* FALSE == Local (instance) handle for randomness */
117 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (MonoArray *seed)
119 HCRYPTPROV provider = 0;
121 /* There is no need to create a container for just random data,
122 so we can use CRYPT_VERIFY_CONTEXT (one call) see:
123 http://blogs.msdn.com/dangriff/archive/2003/11/19/51709.aspx */
125 /* We first try to use the Intel PIII RNG if drivers are present */
126 if (!CryptAcquireContext (&provider, NULL, NULL, PROV_INTEL_SEC, CRYPT_VERIFY_CONTEXT)) {
127 /* not a PIII or no drivers available, use default RSA CSP */
128 if (!CryptAcquireContext (&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFY_CONTEXT)) {
130 /* exception will be thrown in managed code */
134 /* seed the CSP with the supplied buffer (if present) */
135 if ((provider != 0) && (seed)) {
136 guint32 len = mono_array_length (seed);
137 guchar *buf = mono_array_addr (seed, guchar, 0);
138 /* the call we replace the seed with random - this isn't what is
139 expected from the class library user */
140 guchar *data = g_malloc (len);
142 memcpy (data, buf, len);
143 /* add seeding material to the RNG */
144 CryptGenRandom (provider, len, data);
145 /* zeroize and free */
146 memset (data, 0, len);
151 return (gpointer) provider;
155 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpointer handle, MonoArray *arry)
157 HCRYPTPROV provider = (HCRYPTPROV) handle;
158 guint32 len = mono_array_length (arry);
159 guchar *buf = mono_array_addr (arry, guchar, 0);
161 if (!CryptGenRandom (provider, len, buf)) {
162 CryptReleaseContext (provider, 0);
163 /* we may have lost our context with CryptoAPI, but all hope isn't lost yet! */
164 provider = (HCRYPTPROV) ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (NULL);
165 if (!CryptGenRandom (provider, len, buf)) {
166 CryptReleaseContext (provider, 0);
168 /* exception will be thrown in managed code */
171 return (gpointer) provider;
175 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngClose (gpointer handle)
177 CryptReleaseContext ((HCRYPTPROV) handle, 0);
182 #ifndef NAME_DEV_URANDOM
183 #define NAME_DEV_URANDOM "/dev/urandom"
186 static gboolean egd = FALSE;
187 static gint file = -1;
190 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngOpen (void)
192 if (egd || (file >= 0))
195 #if defined (NAME_DEV_URANDOM)
196 file = open (NAME_DEV_URANDOM, O_RDONLY);
199 #if defined (NAME_DEV_RANDOM)
201 file = open (NAME_DEV_RANDOM, O_RDONLY);
205 const char *socket_path = g_getenv("MONO_EGD_SOCKET");
206 egd = (socket_path != NULL);
209 /* TRUE == Global handle for randomness */
214 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (MonoArray *seed)
216 /* if required exception will be thrown in managed code */
217 return ((!egd && (file < 0)) ? NULL : GINT_TO_POINTER (file));
221 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpointer handle, MonoArray *arry)
223 gint file = GPOINTER_TO_INT (handle);
224 guint32 len = mono_array_length (arry);
225 guchar *buf = mono_array_addr (arry, guchar, 0);
228 const char *socket_path = g_getenv ("MONO_EGD_SOCKET");
229 /* exception will be thrown in managed code */
230 if (socket_path == NULL)
232 get_entropy_from_server (socket_path, mono_array_addr (arry, guchar, 0), mono_array_length (arry));
233 return (gpointer) -1;
235 /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */
240 err = read (file, buf + count, len - count);
247 } while (count < len);
250 g_warning("Entropy error! Error in read (%s).", strerror (errno));
251 /* exception will be thrown in managed code */
256 /* We do not support PRNG seeding right now but the class library is this */
262 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngClose (gpointer handle)
266 #endif /* OS definition */