summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>2008-07-21 13:52:06 +0000
committerache <ache@FreeBSD.org>2008-07-21 13:52:06 +0000
commit5ed3228d52c437ca20964c105780d69d2e6892b1 (patch)
tree76fdcd5d3b5825fc0d89a698efff7a825c0ba340 /lib
parent101cbb7cb3db72e499cc35d9319dac30b653d25b (diff)
downloadFreeBSD-src-5ed3228d52c437ca20964c105780d69d2e6892b1.zip
FreeBSD-src-5ed3228d52c437ca20964c105780d69d2e6892b1.tar.gz
Implement arc4random_buf() function
Obtained from: OpenBSD
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/gen/arc4random.315
-rw-r--r--lib/libc/gen/arc4random.c17
2 files changed, 28 insertions, 4 deletions
diff --git a/lib/libc/gen/arc4random.3 b/lib/libc/gen/arc4random.3
index 27c6fd1..5af38ce 100644
--- a/lib/libc/gen/arc4random.3
+++ b/lib/libc/gen/arc4random.3
@@ -35,6 +35,7 @@
.Os
.Sh NAME
.Nm arc4random ,
+.Nm arc4random_buf ,
.Nm arc4random_stir ,
.Nm arc4random_addrandom
.Nd arc4 random number generator
@@ -45,6 +46,8 @@
.Ft u_int32_t
.Fn arc4random "void"
.Ft void
+.Fn arc4random_buf "void *buf" "size_t nbytes"
+.Ft void
.Fn arc4random_stir "void"
.Ft void
.Fn arc4random_addrandom "unsigned char *dat" "int datlen"
@@ -68,6 +71,13 @@ and therefore has twice the range of
and
.Xr random 3 .
.Pp
+.Fn arc4random_buf
+function fills the region
+.Fa buf
+of length
+.Fa nbytes
+with ARC4-derived random data.
+.Pp
The
.Fn arc4random_stir
function reads data from
@@ -78,10 +88,9 @@ and uses it to permute the S-Boxes via
There is no need to call
.Fn arc4random_stir
before using
-.Fn arc4random ,
-since
.Fn arc4random
-automatically initializes itself.
+functions family, since
+they automatically initialize themselves.
.Sh EXAMPLES
The following produces a drop-in replacement for the traditional
.Fn rand
diff --git a/lib/libc/gen/arc4random.c b/lib/libc/gen/arc4random.c
index 6aa2323..7c11da2 100644
--- a/lib/libc/gen/arc4random.c
+++ b/lib/libc/gen/arc4random.c
@@ -164,7 +164,7 @@ arc4_check_init(void)
}
}
-static void
+static inline void
arc4_check_stir(void)
{
if (!rs_stired || arc4_count <= 0) {
@@ -208,6 +208,21 @@ arc4random(void)
return (rnd);
}
+void
+arc4random_buf(void *_buf, size_t n)
+{
+ u_char *buf = (u_char *)_buf;
+
+ THREAD_LOCK();
+ arc4_check_init();
+ while (n--) {
+ arc4_check_stir();
+ buf[n] = arc4_getbyte(&rs);
+ arc4_count--;
+ }
+ THREAD_UNLOCK();
+}
+
#if 0
/*-------- Test code for i386 --------*/
#include <stdio.h>
OpenPOWER on IntegriCloud