2 * Arc4 random number generator for OpenBSD.
3 * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
5 * Modification and redistribution in source and binary forms is
6 * permitted provided that due credit is given to the author and the
7 * OpenBSD project (for instance by leaving this copyright notice
12 * This code is derived from section 17.1 of Applied Cryptography,
13 * second edition, which describes a stream cipher allegedly
14 * compatible with RSA Labs "RC4" cipher (the actual description of
15 * which is a trade secret). The same algorithm is used as a stream
16 * cipher called "arcfour" in Tatu Ylonen's ssh package.
18 * Here the stream cipher has been modified always to include the time
19 * when initializing the state. That makes it impossible to
20 * regenerate the same random sequence twice, so this can't be used
21 * for encryption, but will generate good random numbers.
23 * RC4 is a registered trademark of RSA Laboratories.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: src/lib/libc/gen/arc4random.c,v 1.10 2004/03/24 14:44:57 green Exp $");
29 #include "namespace.h"
30 #include <sys/types.h>
37 #include "libc_private.h"
38 #include "un-namespace.h"
46 static pthread_mutex_t arc4random_mtx
= PTHREAD_MUTEX_INITIALIZER
;
48 #define RANDOMDEV "/dev/urandom"
49 #define THREAD_LOCK() \
52 _pthread_mutex_lock(&arc4random_mtx); \
55 #define THREAD_UNLOCK() \
58 _pthread_mutex_unlock(&arc4random_mtx); \
61 static struct arc4_stream rs
;
62 static int rs_initialized
;
65 static inline u_int8_t
arc4_getbyte(struct arc4_stream
*);
66 static void arc4_stir(struct arc4_stream
*);
70 struct arc4_stream
*as
;
74 for (n
= 0; n
< 256; n
++)
81 arc4_addrandom(as
, dat
, datlen
)
82 struct arc4_stream
*as
;
90 for (n
= 0; n
< 256; n
++) {
93 as
->j
= (as
->j
+ si
+ dat
[n
% datlen
]);
94 as
->s
[as
->i
] = as
->s
[as
->j
];
101 struct arc4_stream
*as
;
107 u_int8_t rnd
[128 - sizeof(struct timeval
) - sizeof(pid_t
)];
110 gettimeofday(&rdat
.tv
, NULL
);
112 fd
= _open(RANDOMDEV
, O_RDONLY
, 0);
114 (void) _read(fd
, rdat
.rnd
, sizeof(rdat
.rnd
));
117 /* fd < 0? Ah, what the heck. We'll just take whatever was on the
120 arc4_addrandom(as
, (void *) &rdat
, sizeof(rdat
));
123 * Throw away the first N bytes of output, as suggested in the
124 * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
125 * by Fluher, Mantin, and Shamir. N=1024 is based on
126 * suggestions in the paper "(Not So) Random Shuffles of RC4"
129 for (n
= 0; n
< 1024; n
++)
133 static inline u_int8_t
135 struct arc4_stream
*as
;
141 as
->j
= (as
->j
+ si
);
146 return (as
->s
[(si
+ sj
) & 0xff]);
149 static inline u_int32_t
151 struct arc4_stream
*as
;
155 val
= arc4_getbyte(as
) << 24;
156 val
|= arc4_getbyte(as
) << 16;
157 val
|= arc4_getbyte(as
) << 8;
158 val
|= arc4_getbyte(as
);
164 arc4_check_init(void)
166 if (!rs_initialized
) {
173 arc4_check_stir(void)
191 arc4random_addrandom(dat
, datlen
)
198 arc4_addrandom(&rs
, dat
, datlen
);
210 rnd
= arc4_getword(&rs
);
217 /*-------- Test code for i386 --------*/
219 #include <machine/pctr.h>
221 main(int argc
, char **argv
)
223 const int iter
= 1000000;
228 for (i
= 0; i
< iter
; i
++)
233 printf("%qd cycles\n", v
);