1 /* $NetBSD: arc4random.c,v 1.8 2005/06/12 05:21:27 lukem Exp $ */
2 /* $OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $ */
5 * Arc4 random number generator for OpenBSD.
6 * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
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 by leaving this copyright notice intact.
14 * This code is derived from section 17.1 of Applied Cryptography,
15 * second edition, which describes a stream cipher allegedly
16 * compatible with RSA Labs "RC4" cipher (the actual description of
17 * which is a trade secret). The same algorithm is used as a stream
18 * cipher called "arcfour" in Tatu Ylonen's ssh package.
20 * Here the stream cipher has been modified always to include the time
21 * when initializing the state. That makes it impossible to
22 * regenerate the same random sequence twice, so this can't be used
23 * for encryption, but will generate good random numbers.
25 * RC4 is a registered trademark of RSA Laboratories.
28 #include <sys/cdefs.h>
29 #if defined(LIBC_SCCS) && !defined(lint)
30 __RCSID("$NetBSD: arc4random.c,v 1.8 2005/06/12 05:21:27 lukem Exp $");
31 #endif /* LIBC_SCCS and not lint */
33 #include "namespace.h"
37 #include <sys/types.h>
38 #include <sys/param.h>
40 #include <sys/sysctl.h>
43 __weak_alias(arc4random
,_arc4random
)
52 static int rs_initialized
;
53 static struct arc4_stream rs
;
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 u_int8_t
arc4_getbyte(struct arc4_stream
*);
59 static inline u_int32_t
arc4_getword(struct arc4_stream
*);
63 struct arc4_stream
*as
;
67 for (n
= 0; n
< 256; n
++)
74 arc4_addrandom(as
, dat
, datlen
)
75 struct arc4_stream
*as
;
83 for (n
= 0; n
< 256; n
++) {
86 as
->j
= (as
->j
+ si
+ dat
[n
% datlen
]);
87 as
->s
[as
->i
] = as
->s
[as
->j
];
95 struct arc4_stream
*as
;
100 u_int rnd
[(128 - sizeof(struct timeval
)) / sizeof(u_int
)];
104 gettimeofday(&rdat
.tv
, NULL
);
105 fd
= open("/dev/urandom", O_RDONLY
);
107 read(fd
, rdat
.rnd
, sizeof(rdat
.rnd
));
116 /* Device could not be opened, we might be chrooted, take
117 * randomness from sysctl. */
122 for (i
= 0; i
< sizeof(rdat
.rnd
) / sizeof(u_int
); i
++) {
124 if (sysctl(mib
, 2, &rdat
.rnd
[i
], &len
, NULL
, 0) == -1)
129 /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take
130 * whatever was on the stack... */
132 arc4_addrandom(as
, (void *) &rdat
, sizeof(rdat
));
135 * Throw away the first N words of output, as suggested in the
136 * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
137 * by Fluher, Mantin, and Shamir. (N = 256 in our case.)
139 for (n
= 0; n
< 256 * 4; n
++)
143 static inline u_int8_t
145 struct arc4_stream
*as
;
151 as
->j
= (as
->j
+ si
);
155 return (as
->s
[(si
+ sj
) & 0xff]);
158 static inline u_int32_t
160 struct arc4_stream
*as
;
163 val
= arc4_getbyte(as
) << 24;
164 val
|= arc4_getbyte(as
) << 16;
165 val
|= arc4_getbyte(as
) << 8;
166 val
|= arc4_getbyte(as
);
173 if (!rs_initialized
) {
181 arc4random_addrandom(dat
, datlen
)
187 arc4_addrandom(&rs
, dat
, datlen
);
195 return arc4_getword(&rs
);
199 /*-------- Test code for i386 --------*/
201 #include <machine/pctr.h>
203 main(int argc
, char **argv
)
205 const int iter
= 1000000;
210 for (i
= 0; i
< iter
; i
++)
215 printf("%qd cycles\n", v
);