[Mono-devel-list] RNG For Win32

Mark Crichton crichton at gimp.org
Tue Dec 9 10:04:56 EST 2003


Ok,

Here it is, the long awaited patch to get the cryptographic RNG into
the Win32 port.  There is some support, this patch cleans up the icalls
and the mcs runtime to convert some of the unmanaged code into managed
code.

Thanks,
Mark Crichton

-------------- next part --------------
Index: mono/metadata/ChangeLog
===================================================================
RCS file: /cvs/public/mono/mono/metadata/ChangeLog,v
retrieving revision 1.1272
diff -u -r1.1272 ChangeLog
--- mono/metadata/ChangeLog	9 Dec 2003 14:30:48 -0000	1.1272
+++ mono/metadata/ChangeLog	9 Dec 2003 14:49:18 -0000
@@ -1,3 +1,9 @@
+2003-12-09  Mark Crichton <crichton at gimp.org>
+
+	* icall.c: removed the GetNonZeroBytes.  We now handle this case
+	in managed code.
+
+	* rand.c, rand.h: Same here.  Also cleaned up the code slightly.
 
 Tue Dec 9 15:36:18 CET 2003 Paolo Molaro <lupus at ximian.com>
 
Index: mono/metadata/icall.c
===================================================================
RCS file: /cvs/public/mono/mono/metadata/icall.c,v
retrieving revision 1.383
diff -u -r1.383 icall.c
--- mono/metadata/icall.c	9 Dec 2003 14:30:48 -0000	1.383
+++ mono/metadata/icall.c	9 Dec 2003 14:49:20 -0000
@@ -4795,7 +4795,6 @@
 	 */
 
 	 "System.Security.Cryptography.RNGCryptoServiceProvider::InternalGetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetBytes,
-	 "System.Security.Cryptography.RNGCryptoServiceProvider::InternalGetNonZeroBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes,
 	
 	/*
 	 * System.Buffer
Index: mono/metadata/rand.c
===================================================================
RCS file: /cvs/public/mono/mono/metadata/rand.c,v
retrieving revision 1.8
diff -u -r1.8 rand.c
--- mono/metadata/rand.c	8 Dec 2003 21:49:25 -0000	1.8
+++ mono/metadata/rand.c	9 Dec 2003 14:49:20 -0000
@@ -59,14 +59,7 @@
        mono_raise_exception (mono_get_exception_execution_engine ("Failed to generate random bytes from CryptAPI"));
 }
 
-void ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes(MonoObject *self, MonoArray *arry)
-{
-   mono_raise_exception (mono_get_exception_not_implemented());
-}
-
-#else
-
-#if defined (NAME_DEV_RANDOM) && defined (HAVE_CRYPT_RNG)
+#elif defined (NAME_DEV_RANDOM) && defined (HAVE_CRYPT_RNG)
 
 #ifndef NAME_DEV_URANDOM
 #define NAME_DEV_URANDOM "/dev/urandom"
@@ -110,42 +103,6 @@
     close(file);
 }
 
-void 
-ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes (MonoObject *self, MonoArray *arry)
-{
-    guint32 len;
-    gint file, i;
-    gint err;
-    guchar byte = 0;
-
-    len = mono_array_length(arry);
-
-    file = open(NAME_DEV_RANDOM, O_RDONLY);
-
-    if (file < 0) {
-        g_warning("Entropy problem! Can't open %s", NAME_DEV_RANDOM);
-
-        /* This needs to be a crypto exception */
-        mono_raise_exception (mono_get_exception_execution_engine ("Failed to open /dev/random device"));
-    }
-
-    for (i = 0; i < len; i++) {
-
-        do {
-            err = read(file, &byte, 1);
-        } while (byte == 0);
-
-        if (err < 0) {
-            g_warning("Entropy error! Error in read.");
-            mono_raise_exception (mono_get_exception_execution_engine ("Failed to read a random byte from /dev/urandom or /dev/random device"));
-        }
-
-        mono_array_set(arry, guchar, i, byte);
-    }
-
-    close(file);
-}
-
 #else
 
 void ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetBytes(MonoObject *self, MonoArray *arry)
@@ -153,11 +110,4 @@
     mono_raise_exception(mono_get_exception_not_implemented());
 }
 
-void ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes(MonoObject *self, MonoArray *arry)
-{
-    mono_raise_exception(mono_get_exception_not_implemented());
-}
-
-#endif // #if defined (NAME_DEV_RANDOM) && defined (HAVE_CRYPT_RNG)
-
-#endif // #if defined (PLATFORM_WIN32)
+#endif /* OS definition */
Index: mono/metadata/rand.h
===================================================================
RCS file: /cvs/public/mono/mono/metadata/rand.h,v
retrieving revision 1.3
diff -u -r1.3 rand.h
--- mono/metadata/rand.h	10 Jan 2003 02:26:36 -0000	1.3
+++ mono/metadata/rand.h	9 Dec 2003 14:49:20 -0000
@@ -14,6 +14,5 @@
 #include <mono/metadata/object.h>
 
 void ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetBytes(MonoObject *self, MonoArray *arry);
-void ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes(MonoObject *self, MonoArray *arry);
 
 #endif
-------------- next part --------------
Index: class/corlib/System.Security.Cryptography/RNGCryptoServiceProvider.cs
===================================================================
RCS file: /cvs/public/mcs/class/corlib/System.Security.Cryptography/RNGCryptoServiceProvider.cs,v
retrieving revision 1.4
diff -u -r1.4 RNGCryptoServiceProvider.cs
--- class/corlib/System.Security.Cryptography/RNGCryptoServiceProvider.cs	10 Jun 2003 00:47:37 -0000	1.4
+++ class/corlib/System.Security.Cryptography/RNGCryptoServiceProvider.cs	9 Dec 2003 14:53:32 -0000
@@ -52,9 +52,6 @@
 		[MethodImplAttribute(MethodImplOptions.InternalCall)]
 		private extern void InternalGetBytes (byte[] data);
 		
-		[MethodImplAttribute(MethodImplOptions.InternalCall)]
-		private extern void InternalGetNonZeroBytes (byte[] data);
-
 		public override void GetBytes (byte[] data) 
 		{
 			InternalGetBytes (data);
@@ -62,7 +59,18 @@
 		
 		public override void GetNonZeroBytes (byte[] data) 
 		{
-			InternalGetNonZeroBytes (data);
+        		byte[] random = new byte [data.Length * 2];
+        		int i = 0;
+        		// one pass should be enough but hey this is random ;-)
+        		while (i < data.Length) {
+                		GetBytes (random);
+                		for (int j=0; j < random.Length; j++) {
+                        		if (i == data.Length)
+                                		break;
+                        		if (random [j] != 0)
+                                		data [i++] = random [j];
+                		}
+        		}
 		}
 		
 		~RNGCryptoServiceProvider () 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 185 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20031209/0bbe8ced/attachment.bin 


More information about the Mono-devel-list mailing list