summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2000-04-03 23:23:48 +0000
committermillert <>2000-04-03 23:23:48 +0000
commitdc35e9219fa6088544a9e0fe13b3baf01ae24d1c (patch)
tree9db8e9f80ab7396b931d742b3f82d4f1fa718c93
parentb741f7cef95c2485e6eaf92dc465eb8dee2d8c51 (diff)
downloadopenbsd-dc35e9219fa6088544a9e0fe13b3baf01ae24d1c.tar.gz
openbsd-dc35e9219fa6088544a9e0fe13b3baf01ae24d1c.tar.bz2
openbsd-dc35e9219fa6088544a9e0fe13b3baf01ae24d1c.zip
Add srandomdev() from FreeBSD for use by sendmail and others.
-rw-r--r--src/lib/libc/stdlib/random.325
-rw-r--r--src/lib/libc/stdlib/random.c47
2 files changed, 69 insertions, 3 deletions
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3
index 9558a672eb..260c239816 100644
--- a/src/lib/libc/stdlib/random.3
+++ b/src/lib/libc/stdlib/random.3
@@ -29,7 +29,7 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" $OpenBSD: random.3,v 1.10 2000/01/19 05:25:43 pjanzen Exp $ 32.\" $OpenBSD: random.3,v 1.11 2000/04/03 23:23:48 millert Exp $
33.\" 33.\"
34.Dd April 19, 1991 34.Dd April 19, 1991
35.Dt RANDOM 3 35.Dt RANDOM 3
@@ -37,6 +37,7 @@
37.Sh NAME 37.Sh NAME
38.Nm random , 38.Nm random ,
39.Nm srandom , 39.Nm srandom ,
40.Nm srandomdev ,
40.Nm initstate , 41.Nm initstate ,
41.Nm setstate 42.Nm setstate
42.Nd better random number generator; routines for changing generators 43.Nd better random number generator; routines for changing generators
@@ -46,6 +47,8 @@
46.Fn random void 47.Fn random void
47.Ft void 48.Ft void
48.Fn srandom "unsigned int seed" 49.Fn srandom "unsigned int seed"
50.Ft void
51.Fn srandomdev void
49.Ft char * 52.Ft char *
50.Fn initstate "unsigned int seed" "char *state" "size_t n" 53.Fn initstate "unsigned int seed" "char *state" "size_t n"
51.Ft char * 54.Ft char *
@@ -92,6 +95,19 @@ with
92as the seed. 95as the seed.
93.Pp 96.Pp
94The 97The
98.Fn srandomdev
99routine initialize a state array using the
100.Xr arandom 4
101random number device which returns good random numbers,
102suitable for cryptographic use.
103Note that this particular seeding procedure can generate
104states which are impossible to reproduce by calling
105.Fn srandom
106with any value, since the succeeding terms in the
107state buffer are no longer derived from the LC algorithm applied to
108a fixed seed.
109.Pp
110The
95.Fn initstate 111.Fn initstate
96routine allows a state array, passed in as an argument, to be initialized 112routine allows a state array, passed in as an argument, to be initialized
97for future use. The size of the state array (in bytes) is used by 113for future use. The size of the state array (in bytes) is used by
@@ -153,7 +169,8 @@ messages are printed on the standard error output.
153.Sh SEE ALSO 169.Sh SEE ALSO
154.Xr arc4random 3 , 170.Xr arc4random 3 ,
155.Xr drand48 3 , 171.Xr drand48 3 ,
156.Xr rand 3 172.Xr rand 3 ,
173.Xr random 4 ,
157.Sh STANDARDS 174.Sh STANDARDS
158The 175The
159.Fn random , 176.Fn random ,
@@ -163,6 +180,10 @@ and
163.Fn setstate 180.Fn setstate
164functions conform to 181functions conform to
165.St -xpg4.2 . 182.St -xpg4.2 .
183.Pp
184The
185.Fn srandomdev
186function is an extension.
166.Sh HISTORY 187.Sh HISTORY
167These 188These
168functions appeared in 189functions appeared in
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c
index 79344f30f1..7c6e5f7eb9 100644
--- a/src/lib/libc/stdlib/random.c
+++ b/src/lib/libc/stdlib/random.c
@@ -32,11 +32,15 @@
32 */ 32 */
33 33
34#if defined(LIBC_SCCS) && !defined(lint) 34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: random.c,v 1.6 1998/02/07 02:16:25 millert Exp $"; 35static char *rcsid = "$OpenBSD: random.c,v 1.7 2000/04/03 23:23:48 millert Exp $";
36#endif /* LIBC_SCCS and not lint */ 36#endif /* LIBC_SCCS and not lint */
37 37
38#include <sys/types.h>
39#include <sys/time.h>
40#include <fcntl.h>
38#include <stdio.h> 41#include <stdio.h>
39#include <stdlib.h> 42#include <stdlib.h>
43#include <unistd.h>
40 44
41/* 45/*
42 * random.c: 46 * random.c:
@@ -220,6 +224,47 @@ srandom(x)
220} 224}
221 225
222/* 226/*
227 * srandomdev:
228 *
229 * Many programs choose the seed value in a totally predictable manner.
230 * This often causes problems. We seed the generator using the much more
231 * secure arandom(4) interface. Note that this particular seeding
232 * procedure can generate states which are impossible to reproduce by
233 * calling srandom() with any value, since the succeeding terms in the
234 * state buffer are no longer derived from the LC algorithm applied to
235 * a fixed seed.
236 */
237void
238srandomdev()
239{
240 int fd;
241 size_t len;
242
243 if (rand_type == TYPE_0)
244 len = sizeof(state[0]);
245 else
246 len = rand_deg * sizeof(state[0]);
247
248 if ((fd = open("/dev/arandom", O_RDONLY, 0)) != -1 &&
249 read(fd, (void *) state, len) == (ssize_t) len) {
250 close(fd);
251 } else {
252 struct timeval tv;
253 u_int junk;
254
255 /* XXX - this could be better */
256 gettimeofday(&tv, NULL);
257 srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk);
258 return;
259 }
260
261 if (rand_type != TYPE_0) {
262 fptr = &state[rand_sep];
263 rptr = &state[0];
264 }
265}
266
267/*
223 * initstate: 268 * initstate:
224 * 269 *
225 * Initialize the state information in the given array of n bytes for future 270 * Initialize the state information in the given array of n bytes for future