Implement arc4random_buf() function

Obtained from:  OpenBSD
This commit is contained in:
Andrey A. Chernov 2008-07-21 13:52:06 +00:00
parent b6634bf8d2
commit bc6847e225
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=180657
2 changed files with 28 additions and 4 deletions

View File

@ -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

View File

@ -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>