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 $ */
6 * Arc4 random number generator for OpenBSD.
7 * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
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.
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.
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.
26 * RC4 is a registered trademark of RSA Laboratories.
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #ifdef __ARC4RANDOM_USE_ERANDOM__
37 #include <sys/sysctl.h>
47 static int rs_initialized;
48 static struct arc4_stream rs;
50 static __inline__ void arc4_init(struct arc4_stream *);
51 static __inline__ void arc4_addrandom(struct arc4_stream *, u_char *, int);
52 static void arc4_stir(struct arc4_stream *);
53 static __inline__ uint8_t arc4_getbyte(struct arc4_stream *);
54 static __inline__ uint32_t arc4_getword(struct arc4_stream *);
56 static __inline__ void
58 struct arc4_stream *as;
62 for (n = 0; n < 256; n++)
68 static __inline__ void
69 arc4_addrandom(as, dat, datlen)
70 struct arc4_stream *as;
78 for (n = 0; n < 256; n++) {
81 as->j = (as->j + si + dat[n % datlen]);
82 as->s[as->i] = as->s[as->j];
90 struct arc4_stream *as;
95 uint rnd[(128 - sizeof(struct timeval)) / sizeof(uint)];
99 gettimeofday(&rdat.tv, NULL);
100 fd = open("/dev/urandom", O_RDONLY);
102 read(fd, rdat.rnd, sizeof(rdat.rnd));
105 #ifdef __ARC4RANDOM_USE_ERANDOM__
111 /* Device could not be opened, we might be chrooted, take
112 * randomness from sysctl. */
115 mib[1] = KERN_RANDOM;
116 mib[2] = RANDOM_ERANDOM;
118 for (i = 0; i < sizeof(rdat.rnd) / sizeof(uint); i++) {
120 if (sysctl(mib, 3, &rdat.rnd[i], &len, NULL, 0) == -1)
126 arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
129 * Throw away the first N words of output, as suggested in the
130 * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
131 * by Fluher, Mantin, and Shamir.
132 * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
133 * N = 256 in our case.
135 for (n = 0; n < 256 * 4; n++)
139 static __inline__ uint8_t
141 struct arc4_stream *as;
147 as->j = (as->j + si);
151 return (as->s[(si + sj) & 0xff]);
154 static __inline__ uint32_t
156 struct arc4_stream *as;
159 val = arc4_getbyte(as) << 24;
160 val |= arc4_getbyte(as) << 16;
161 val |= arc4_getbyte(as) << 8;
162 val |= arc4_getbyte(as);
167 arc4random_stir(void)
169 if (!rs_initialized) {
175 libc_hidden_def(arc4random_stir)
178 arc4random_addrandom(u_char *dat, int datlen)
182 arc4_addrandom(&rs, dat, datlen);
190 return arc4_getword(&rs);
194 /*-------- Test code --------*/
200 random_number = arc4random() % 65536;
201 printf("%d\n", random_number);