[JDEV] Still another patch ... (seed the rand() function)

Matthias Wimmer m at tthias.net
Mon Oct 13 18:00:34 CDT 2003


Hi!

Matthias Wimmer schrieb am 2003-10-13 23:00:18:
> But as I said: you're right. The hole thing with rand() is not the best
> solution. Maybe it would be a good idea to use the RAND_*() functions of
> openssl if compiled with SSL support.

The attached patch would use RAND_pseudo_bytes() to get pseudo random
bytes seeded from /dev/urandom. Using cryptographically strong bytes
(the function RAND_bytes()) shouldn't be needed here and most of the
time you get them with this call too.

But is it needed? I don't see any benefit for an attacker to predict
the challenge - it just has to be unique.


Tot kijk
    Matthias

-- 
For kibibytes see:
http://www.iec.ch/online_news/etech/arch_2003/etech_0503/focus.htm
-------------- next part --------------
diff -Naur jadc2s/clients.c jadc2s-rand/clients.c
--- jadc2s/clients.c	2003-03-19 18:13:59.000000000 +0100
+++ jadc2s-rand/clients.c	2003-10-14 00:47:08.000000000 +0200
@@ -49,6 +49,7 @@
     int i = 0, error;
     char *header, *header_from, header_id[30], header_end[3];
     char sid[24];
+    int randval;
 
     /* don't do anything if we're about to bail out */
     if(c->depth < 0)
@@ -157,8 +158,17 @@
             return;
         }
 
+#ifdef USE_SSL
+	if (RAND_pseudo_bytes((unsigned char*)&randval, sizeof(randval))<0)
+	{
+            log_write(c->c2s->log, LOG_ERR, "RAND_pseudo_bytes() not supported by the current RAND method");
+            exit(1);
+	}
+#else
         /* XXX fancier algo for id generation? */
-        snprintf(sid, 24, "%d", rand());
+	randval = rand();
+#endif
+        snprintf(sid, 24, "%d", randval);
 
         header_from = malloc( 9 + strlen( c->local_id ) );
         sprintf(header_from, " from='%s'", c->local_id);
diff -Naur jadc2s/jadc2s.c jadc2s-rand/jadc2s.c
--- jadc2s/jadc2s.c	2002-10-23 00:01:42.000000000 +0200
+++ jadc2s-rand/jadc2s.c	2003-10-14 00:38:45.000000000 +0200
@@ -198,6 +198,12 @@
         return 1;
     }
 
+    /* seed the random number generator function */
+    /* openssl will use /dev/urandom to seed */
+#ifndef USE_SSL
+    srand(time(NULL));
+#endif
+
     /* start logging */
     c2s->log = log_new("jadc2s");
     log_write(c2s->log, LOG_NOTICE, "starting up");
diff -Naur jadc2s/jadc2s.h jadc2s-rand/jadc2s.h
--- jadc2s/jadc2s.h	2002-10-15 01:45:08.000000000 +0200
+++ jadc2s-rand/jadc2s.h	2003-10-14 00:47:36.000000000 +0200
@@ -4,6 +4,7 @@
 
 #ifdef USE_SSL
 # include <openssl/ssl.h>
+# include <openssl/rand.h>
 #endif
 
 /****** First notes by jer on 2002/03/17: ******
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <https://www.jabber.org/jdev/attachments/20031014/4bbf1855/attachment-0002.pgp>


More information about the JDev mailing list