OSDN Git Service

836b79f6c4d94661d8b93b8e2dc757da4fb77bb9
[uclinux-h8/uclibc-ng.git] / libc / stdlib / arc4random.c
1 /*      $$$: arc4random.c 2005/02/08 robert */
2 /*      $NetBSD: arc4random.c,v 1.5.2.1 2004/03/26 22:52:50 jmc Exp $   */
3 /*      $OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $ */
4
5 /*
6  * Arc4 random number generator for OpenBSD.
7  * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
8  *
9  * Modification and redistribution in source and binary forms is
10  * permitted provided that due credit is given to the author and the
11  * OpenBSD project by leaving this copyright notice 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 <features.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #ifdef __ARC4RANDOM_USE_ERANDOM__
37 #include <sys/sysctl.h>
38 //libc_hidden_proto(sysctl)
39 #endif
40
41 libc_hidden_proto(open)
42 libc_hidden_proto(read)
43 libc_hidden_proto(close)
44 libc_hidden_proto(gettimeofday)
45
46 struct arc4_stream {
47         uint8_t i;
48         uint8_t j;
49         uint8_t s[256];
50 };
51
52 static int    rs_initialized;
53 static struct arc4_stream rs;
54
55 static inline void arc4_init(struct arc4_stream *);
56 static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
57 static void arc4_stir(struct arc4_stream *);
58 static inline uint8_t arc4_getbyte(struct arc4_stream *);
59 static inline uint32_t arc4_getword(struct arc4_stream *);
60
61 static inline void
62 arc4_init(as)
63         struct arc4_stream *as;
64 {
65         int     n;
66
67         for (n = 0; n < 256; n++)
68                 as->s[n] = n;
69         as->i = 0;
70         as->j = 0;
71 }
72
73 static inline void
74 arc4_addrandom(as, dat, datlen)
75         struct arc4_stream *as;
76         u_char *dat;
77         int     datlen;
78 {
79         int     n;
80         uint8_t si;
81
82         as->i--;
83         for (n = 0; n < 256; n++) {
84                 as->i = (as->i + 1);
85                 si = as->s[as->i];
86                 as->j = (as->j + si + dat[n % datlen]);
87                 as->s[as->i] = as->s[as->j];
88                 as->s[as->j] = si;
89         }
90         as->j = as->i;
91 }
92
93 static void
94 arc4_stir(as)
95         struct arc4_stream *as;
96 {
97         int     fd;
98         struct {
99                 struct timeval tv;
100                 uint rnd[(128 - sizeof(struct timeval)) / sizeof(uint)];
101         }       rdat;
102         int     n;
103
104         gettimeofday(&rdat.tv, NULL);
105         fd = open("/dev/urandom", O_RDONLY);
106         if (fd != -1) {
107                 read(fd, rdat.rnd, sizeof(rdat.rnd));
108                 close(fd);
109         }
110 #ifdef __ARC4RANDOM_USE_ERANDOM__
111         else {
112                 int mib[3];
113                 uint i;
114                 size_t len;
115
116                 /* Device could not be opened, we might be chrooted, take
117                  * randomness from sysctl. */
118
119                 mib[0] = CTL_KERN;
120                 mib[1] = KERN_RANDOM;
121                 mib[2] = RANDOM_ERANDOM;
122
123                 for (i = 0; i < sizeof(rdat.rnd) / sizeof(uint); i++) {
124                         len = sizeof(uint);
125                         if (sysctl(mib, 3, &rdat.rnd[i], &len, NULL, 0) == -1)
126                                 break;
127                 }
128         }
129 #endif
130
131         arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
132
133         /*
134          * Throw away the first N words of output, as suggested in the
135          * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
136          * by Fluher, Mantin, and Shamir.
137          * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
138          * N = 256 in our case.
139          */
140         for (n = 0; n < 256 * 4; n++)
141                 arc4_getbyte(as);
142 }
143
144 static inline uint8_t
145 arc4_getbyte(as)
146         struct arc4_stream *as;
147 {
148         uint8_t si, sj;
149
150         as->i = (as->i + 1);
151         si = as->s[as->i];
152         as->j = (as->j + si);
153         sj = as->s[as->j];
154         as->s[as->i] = sj;
155         as->s[as->j] = si;
156         return (as->s[(si + sj) & 0xff]);
157 }
158
159 static inline uint32_t
160 arc4_getword(as)
161         struct arc4_stream *as;
162 {
163         uint32_t val;
164         val = arc4_getbyte(as) << 24;
165         val |= arc4_getbyte(as) << 16;
166         val |= arc4_getbyte(as) << 8;
167         val |= arc4_getbyte(as);
168         return val;
169 }
170
171 libc_hidden_proto(arc4random_stir)
172 void
173 arc4random_stir(void)
174 {
175         if (!rs_initialized) {
176                 arc4_init(&rs);
177                 rs_initialized = 1;
178         }
179         arc4_stir(&rs);
180 }
181 libc_hidden_def(arc4random_stir)
182
183 void
184 arc4random_addrandom(u_char *dat, int datlen)
185 {
186         if (!rs_initialized)
187                 arc4random_stir();
188         arc4_addrandom(&rs, dat, datlen);
189 }
190
191 uint32_t
192 arc4random(void)
193 {
194         if (!rs_initialized)
195                 arc4random_stir();
196         return arc4_getword(&rs);
197 }
198
199 #if 0
200 /*-------- Test code --------*/
201 #include <stdlib.h>
202 #include <stdio.h>
203
204 int main(void) {
205     int random_number;
206     random_number = arc4random() % 65536;
207     printf("%d\n", random_number);
208     return 0;
209 }
210 #endif