Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libcr / gen / arc4random.c
1 /* $FreeBSD: src/lib/libc/gen/arc4random.c,v 1.4 2000/01/27 23:06:13 jasone Exp $ */
2 /* $DragonFly: src/lib/libcr/gen/Attic/arc4random.c,v 1.2 2003/06/17 04:26:42 dillon Exp $ */
3
4 /*
5  * Arc4 random number generator for OpenBSD.
6  * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
7  *
8  * Modification and redistribution in source and binary forms is
9  * permitted provided that due credit is given to the author and the
10  * OpenBSD project (for instance by leaving this copyright notice
11  * intact).
12  */
13
14 /*
15  * This code is derived from section 17.1 of Applied Cryptography,
16  * second edition, which describes a stream cipher allegedly
17  * compatible with RSA Labs "RC4" cipher (the actual description of
18  * which is a trade secret).  The same algorithm is used as a stream
19  * cipher called "arcfour" in Tatu Ylonen's ssh package.
20  *
21  * Here the stream cipher has been modified always to include the time
22  * when initializing the state.  That makes it impossible to
23  * regenerate the same random sequence twice, so this can't be used
24  * for encryption, but will generate good random numbers.
25  *
26  * RC4 is a registered trademark of RSA Laboratories.
27  */
28
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34
35 struct arc4_stream {
36         u_int8_t i;
37         u_int8_t j;
38         u_int8_t s[256];
39 };
40
41 static int rs_initialized;
42 static struct arc4_stream rs;
43
44 static inline void
45 arc4_init(as)
46         struct arc4_stream *as;
47 {
48         int     n;
49
50         for (n = 0; n < 256; n++)
51                 as->s[n] = n;
52         as->i = 0;
53         as->j = 0;
54 }
55
56 static inline void
57 arc4_addrandom(as, dat, datlen)
58         struct arc4_stream *as;
59         u_char *dat;
60         int     datlen;
61 {
62         int     n;
63         u_int8_t si;
64
65         as->i--;
66         for (n = 0; n < 256; n++) {
67                 as->i = (as->i + 1);
68                 si = as->s[as->i];
69                 as->j = (as->j + si + dat[n % datlen]);
70                 as->s[as->i] = as->s[as->j];
71                 as->s[as->j] = si;
72         }
73 }
74
75 static void
76 arc4_stir(as)
77         struct arc4_stream *as;
78 {
79         int     fd;
80         struct {
81                 struct timeval tv;
82                 pid_t pid;
83                 u_int8_t rnd[128 - sizeof(struct timeval) - sizeof(pid_t)];
84         }       rdat;
85
86         gettimeofday(&rdat.tv, NULL);
87         rdat.pid = getpid();
88         fd = _open("/dev/urandom", O_RDONLY, 0);
89         if (fd >= 0) {
90                 (void) _read(fd, rdat.rnd, sizeof(rdat.rnd));
91                 _close(fd);
92         }
93         /* fd < 0?  Ah, what the heck. We'll just take whatever was on the
94          * stack... */
95
96         arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
97 }
98
99 static inline u_int8_t
100 arc4_getbyte(as)
101         struct arc4_stream *as;
102 {
103         u_int8_t si, sj;
104
105         as->i = (as->i + 1);
106         si = as->s[as->i];
107         as->j = (as->j + si);
108         sj = as->s[as->j];
109         as->s[as->i] = sj;
110         as->s[as->j] = si;
111         return (as->s[(si + sj) & 0xff]);
112 }
113
114 static inline u_int32_t
115 arc4_getword(as)
116         struct arc4_stream *as;
117 {
118         u_int32_t val;
119         val = arc4_getbyte(as) << 24;
120         val |= arc4_getbyte(as) << 16;
121         val |= arc4_getbyte(as) << 8;
122         val |= arc4_getbyte(as);
123         return val;
124 }
125
126 void
127 arc4random_stir()
128 {
129         if (!rs_initialized) {
130                 arc4_init(&rs);
131                 rs_initialized = 1;
132         }
133         arc4_stir(&rs);
134 }
135
136 void
137 arc4random_addrandom(dat, datlen)
138         u_char *dat;
139         int     datlen;
140 {
141         if (!rs_initialized)
142                 arc4random_stir();
143         arc4_addrandom(&rs, dat, datlen);
144 }
145
146 u_int32_t
147 arc4random()
148 {
149         if (!rs_initialized)
150                 arc4random_stir();
151         return arc4_getword(&rs);
152 }
153
154 #if 0
155 /*-------- Test code for i386 --------*/
156 #include <stdio.h>
157 #include <machine/pctr.h>
158 int
159 main(int argc, char **argv)
160 {
161         const int iter = 1000000;
162         int     i;
163         pctrval v;
164
165         v = rdtsc();
166         for (i = 0; i < iter; i++)
167                 arc4random();
168         v = rdtsc() - v;
169         v /= iter;
170
171         printf("%qd cycles\n", v);
172 }
173 #endif