2 * rand.c: System.Security.Cryptography.RNGCryptoServiceProvider support
5 * Mark Crichton (crichton@gimp.org)
7 * (C) 2001 Ximian, Inc.
12 /* Ok, the exception handling is bogus. I need to work on that */
16 #include <sys/types.h>
21 #include <mono/metadata/object.h>
22 #include <mono/metadata/rand.h>
23 #include <mono/metadata/exception.h>
25 #if defined (NAME_DEV_RANDOM) && defined (HAVE_CRYPT_RNG)
27 #ifndef NAME_DEV_URANDOM
28 #define NAME_DEV_URANDOM "/dev/urandom"
32 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetBytes (MonoObject *self, MonoArray *arry)
40 len = mono_array_length(arry);
41 buf = mono_array_addr(arry, guchar, 0);
43 file = open (NAME_DEV_URANDOM, O_RDONLY);
46 file = open (NAME_DEV_RANDOM, O_RDONLY);
49 g_warning ("Entropy problem! Can't open %s or %s", NAME_DEV_URANDOM, NAME_DEV_RANDOM);
51 /* This needs to be a crypto exception */
52 mono_raise_exception(mono_get_exception_not_implemented());
55 /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */
58 err = read(file, buf + count, len - count);
60 } while (err >= 0 && count < len);
63 g_warning("Entropy error! Error in read.");
64 mono_raise_exception(mono_get_exception_not_implemented());
71 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes (MonoObject *self, MonoArray *arry)
78 len = mono_array_length(arry);
80 file = open(NAME_DEV_RANDOM, O_RDONLY);
83 g_warning("Entropy problem! Can't open %s", NAME_DEV_RANDOM);
85 /* This needs to be a crypto exception */
86 mono_raise_exception(mono_get_exception_not_implemented());
89 for (i = 0; i < len; i++) {
92 err = read(file, &byte, 1);
96 g_warning("Entropy error! Error in read.");
97 mono_raise_exception(mono_get_exception_not_implemented());
100 mono_array_set(arry, guchar, i, byte);
106 /* This needs to change when I do the Win32 support... */
108 void ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetBytes(MonoObject *self, MonoArray *arry)
110 mono_raise_exception(mono_get_exception_not_implemented());
113 void ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_InternalGetNonZeroBytes(MonoObject *self, MonoArray *arry)
115 mono_raise_exception(mono_get_exception_not_implemented());