lib: random utilities
Subsystems (2)
PATCH 1/3 lib: random utilities
From: Davidlohr Bueso <[email protected]>
Add a random number(s) generator specific file. The intial functions are based on what libuuid provide. I did some modifications like avoid WIN32 checks - this is util-LINUX.
--- include/randutils.h | 19 +++++++++ lib/Makefile.am | 2 + lib/randutils.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 0 deletions(-) create mode 100644 include/randutils.h create mode 100644 lib/randutils.c
include/randutils.h new file +19 -0
@@ -0,0 +1,19 @@ +#ifndef UTIL_LINUX_RANDUTILS +#define UTIL_LINUX_RANDUTILS + +#include <sys/syscall.h> + +#ifdef HAVE_SRANDOM +#define srand (x) srandom(x) +#define rand () random() +#endif + +#if defined(__linux__) && defined(__NR_gettid) && defined(HAVE_JRAND48 ) +#define DO_JRAND_MIX +static unsigned short jrand_seed[3 ]; +#endif + +int random_get_fd (void ); +void random_get_bytes (void *buf,int nbytes); + +#endif
lib/Makefile.am +2 -0
@@ -10,6 +10,7 @@ noinst_PROGRAMS = \ test_ismounted \ test_mangle \ test_procutils \ + test_randutils \ test_strutils \ test_tt \ test_wholedisk
@@ -30,6 +31,7 @@ test_at_SOURCES = at.c test_at_CFLAGS = -DTEST_PROGRAM_AT test_strutils_SOURCES = strutils.c +test_randutils_SOURCES = randutils.c test_procutils_SOURCES = procutils.c if LINUX
lib/randutils.c new file +104 -0
104 lines not on this page · raw message
Signature · 1 line
1.7.4.1
Karel Zak
maintainerOn Thu, Apr 05, 2012 at 11:52:04PM +0200, Davidlohr Bueso wrote:
v> +/*
Quoted 33 lines
+ * Generate a stream of random nbytes into buf.
+ * Use/dev/urandomif possible, and if not,
+ * use glibc pseudo-random functions.
+ */lib/randutils.c new file +29
-0+void random_get_bytes (void *buf,int nbytes) +{ +int i, n = nbytes, fd =random_get_fd (); +int lose_counter =0 ; +unsigned char *cp = (unsigned char *) buf; + +if (fd >=0 ) { +while (n >0 ) { + i =read (fd, cp, n); +if (i <=0 ) { +if (lose_counter++ >16 ) +break ; +continue ; + } + n -= i; + cp += i; + lose_counter =0 ; + } + } + +/* +* We do this all the time, but this is the only source of +* randomness if /dev/random/urandom is out to lunch. +*/ +for (cp = buf, i =0 ; i < nbytes; i++) + *cp++ ^= (rand () >>7 ) &0xFF ; + +#ifdef DO_JRAND_MIX + {
lib/randutils.c line 86
Quoted 10 lines
lib/randutils.c new file +10
-0+unsigned short tmp_seed[3 ]; + +memcpy (tmp_seed, jrand_seed,sizeof (tmp_seed)); + jrand_seed[2 ] = jrand_seed[2 ] ^syscall (__NR_gettid); +for (cp = buf, i =0 ; i < nbytes; i++) + *cp++ ^= (jrand48 (tmp_seed) >>7 ) &0xFF ; +memcpy (jrand_seed, tmp_seed, + sizeof(jrand_seed)-sizeof(unsigned short)); + } +#endif
close(fd);
lib/randutils.c line 89
Quoted 3 lines
lib/randutils.c new file +3
-0+ +return ; +}
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Karel Zak
maintainerQuoted 6 lines · Davidlohr Bueso
include/randutils.h | 19 +++++++++ lib/Makefile.am | 2 + lib/randutils.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 0 deletions(-) create mode 100644 include/randutils.h create mode 100644 lib/randutils.c
Applied with some changes, thanks.
include/randutils.h line 14
Quoted 5 lines
+++
b/include/randutils.hinclude/randutils.h new file +4
-0+#if defined(__linux__) && defined(__NR_gettid) && defined(HAVE_JRAND48 ) +#define DO_JRAND_MIX +static unsigned short jrand_seed[3 ]; +#endif
- add static buffer to header file does not make sense
- TLS (thread local storage) is necessary there if you want to use the code in a shared library (libuuid).
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com