[PATCH] arc4random
Here's a patch I sent a couple days ago that may have been overlooked. Thanks, - Mark Index: configure.ac =================================================================== --- configure.ac (revision 166) +++ configure.ac (working copy) @@ -191,7 +191,7 @@ AC_CHECK_DECLS([FD_COPY], [], [], [[#include <sys/select.h>]]) AC_CHECK_DECLS([SIGEMT], [], [], [[#include <signal.h>]]) AC_CHECK_DECLS([VQ_UPDATE, VQ_VERYLOWDISK], [], [], [[#include <sys/mount.h>]]) -AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time malloc_create_zone sysconf]) +AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time malloc_create_zone sysconf arc4random]) AC_CHECK_DECLS([POSIX_SPAWN_START_SUSPENDED], [have_posix_spawn_start_suspended=true], Index: testing/queue_finalizer.c =================================================================== --- testing/queue_finalizer.c (revision 166) +++ testing/queue_finalizer.c (working copy) @@ -32,6 +32,35 @@ void *ctxt_magic; +/* +* Based on ISC BIND lib/isc/random.c +*/ +#ifndef HAVE_ARC4RANDOM +static uint32_t +arc4random(void) +{ + unsigned int pid; + static int arc4random_init; + + /* + * The low bits of pid generally change faster. + * Xor them with the high bits of time which change slowly. + */ + if (!arc4random_init) { + pid = getpid(); + pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff); + srand(time(NULL) ^ pid); + arc4random_init = 1; + } + + /* + * rand()'s lower bits are not random. + * rand()'s upper bit is zero. + */ + return ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000); +} +#endif /* ! HAVE_ARC4RANDOM */ + static void finalizer(void *ctxt) {
On 19 Nov 2009, at 02:41, Mark Heily wrote:
Here's a patch I sent a couple days ago that may have been overlooked.
I did receive it, but given our conversation about it just being for testing purposes in a specific tool and not an issue of library code, I wonder if we shouldn't just make queue_finalizer.c use sranddev() and srand(). Does Solaris have an sranddev()? Jordan, would you object to us just moving to the more portable API entirely in the test tool as opposed to trying to emulate this BSD API? Robert
Thanks,
- Mark
Index: configure.ac =================================================================== --- configure.ac (revision 166) +++ configure.ac (working copy) @@ -191,7 +191,7 @@ AC_CHECK_DECLS([FD_COPY], [], [], [[#include <sys/select.h>]]) AC_CHECK_DECLS([SIGEMT], [], [], [[#include <signal.h>]]) AC_CHECK_DECLS([VQ_UPDATE, VQ_VERYLOWDISK], [], [], [[#include <sys/mount.h>]]) -AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time malloc_create_zone sysconf]) +AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time malloc_create_zone sysconf arc4random])
AC_CHECK_DECLS([POSIX_SPAWN_START_SUSPENDED], [have_posix_spawn_start_suspended=true], Index: testing/queue_finalizer.c =================================================================== --- testing/queue_finalizer.c (revision 166) +++ testing/queue_finalizer.c (working copy) @@ -32,6 +32,35 @@
void *ctxt_magic;
+/* +* Based on ISC BIND lib/isc/random.c +*/ +#ifndef HAVE_ARC4RANDOM +static uint32_t +arc4random(void) +{ + unsigned int pid; + static int arc4random_init; + + /* + * The low bits of pid generally change faster. + * Xor them with the high bits of time which change slowly. + */ + if (!arc4random_init) { + pid = getpid(); + pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff); + srand(time(NULL) ^ pid); + arc4random_init = 1; + } + + /* + * rand()'s lower bits are not random. + * rand()'s upper bit is zero. + */ + return ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000); +} +#endif /* ! HAVE_ARC4RANDOM */ + static void finalizer(void *ctxt) {
No, it doesn't seem supported. /dev/urandom && /dev/random is supported though, and seems to be on all of OS X, FreeBSD and Linux platforms (and more), perhaps I'm missing something, but couldn't we simply read random data from that source instead? Joakim On 19 nov 2009, at 08.44, Robert N. M. Watson wrote:
Does Solaris have an sranddev()?
On Thu, 19 Nov 2009, Joakim Johansson wrote:
No, it doesn't seem supported.
/dev/urandom && /dev/random is supported though, and seems to be on all of OS X, FreeBSD and Linux platforms (and more), perhaps I'm missing something, but couldn't we simply read random data from that source instead?
The temptation would be to provide a compat shim for sranddev() for systems that don't implement it. Robert N M Watson Computer Laboratory University of Cambridge
Joakim
On 19 nov 2009, at 08.44, Robert N. M. Watson wrote:
Does Solaris have an sranddev()?
Hi, Paolo Bonzini schrieb:
On 11/19/2009 09:35 AM, Robert Watson wrote:
The temptation would be to provide a compat shim for sranddev() for systems that don't implement it. Makes sense, but what's wrong with just using srand? :-)
I aggree. sranddev() isn't present on Linux. There's only srand(), and rand() and rand_r(), of course. ciao, Mario
If the random numbers are only used for testing and don't need to be cryptographically strong, I don't have any particular objection if Kevin doesn't. - Jordan On Nov 18, 2009, at 11:44 PM, Robert N. M. Watson wrote:
On 19 Nov 2009, at 02:41, Mark Heily wrote:
Here's a patch I sent a couple days ago that may have been overlooked.
I did receive it, but given our conversation about it just being for testing purposes in a specific tool and not an issue of library code, I wonder if we shouldn't just make queue_finalizer.c use sranddev() and srand(). Does Solaris have an sranddev()? Jordan, would you object to us just moving to the more portable API entirely in the test tool as opposed to trying to emulate this BSD API?
Robert
Thanks,
- Mark
Index: configure.ac =================================================================== --- configure.ac (revision 166) +++ configure.ac (working copy) @@ -191,7 +191,7 @@ AC_CHECK_DECLS([FD_COPY], [], [], [[#include <sys/select.h>]]) AC_CHECK_DECLS([SIGEMT], [], [], [[#include <signal.h>]]) AC_CHECK_DECLS([VQ_UPDATE, VQ_VERYLOWDISK], [], [], [[#include <sys/mount.h>]]) -AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time malloc_create_zone sysconf]) +AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time malloc_create_zone sysconf arc4random])
AC_CHECK_DECLS([POSIX_SPAWN_START_SUSPENDED], [have_posix_spawn_start_suspended=true], Index: testing/queue_finalizer.c =================================================================== --- testing/queue_finalizer.c (revision 166) +++ testing/queue_finalizer.c (working copy) @@ -32,6 +32,35 @@
void *ctxt_magic;
+/* +* Based on ISC BIND lib/isc/random.c +*/ +#ifndef HAVE_ARC4RANDOM +static uint32_t +arc4random(void) +{ + unsigned int pid; + static int arc4random_init; + + /* + * The low bits of pid generally change faster. + * Xor them with the high bits of time which change slowly. + */ + if (!arc4random_init) { + pid = getpid(); + pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff); + srand(time(NULL) ^ pid); + arc4random_init = 1; + } + + /* + * rand()'s lower bits are not random. + * rand()'s upper bit is zero. + */ + return ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000); +} +#endif /* ! HAVE_ARC4RANDOM */ + static void finalizer(void *ctxt) {
participants (7)
-
Joakim Johansson
-
Jordan K. Hubbard
-
Mario Schwalbe
-
Mark Heily
-
Paolo Bonzini
-
Robert N. M. Watson
-
Robert Watson