Home | History | Annotate | Line # | Download | only in gen
arc4random.c revision 1.2
      1 /*	$NetBSD: arc4random.c,v 1.2 2002/05/24 04:53:07 itojun Exp $	*/
      2 /*	$OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $	*/
      3 
      4 /*
      5  * Arc4 random number generator for OpenBSD.
      6  * Copyright 1996 David Mazieres <dm (at) 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 by leaving this copyright notice intact.
     11  */
     12 
     13 /*
     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.
     19  *
     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.
     24  *
     25  * RC4 is a registered trademark of RSA Laboratories.
     26  */
     27 
     28 #include <fcntl.h>
     29 #include <stdlib.h>
     30 #include <unistd.h>
     31 #include <sys/types.h>
     32 #include <sys/param.h>
     33 #include <sys/time.h>
     34 #include <sys/sysctl.h>
     35 
     36 #ifdef __GNUC__
     37 #define inline __inline
     38 #else				/* !__GNUC__ */
     39 #define inline
     40 #endif				/* !__GNUC__ */
     41 
     42 struct arc4_stream {
     43 	u_int8_t i;
     44 	u_int8_t j;
     45 	u_int8_t s[256];
     46 };
     47 
     48 int     rs_initialized;
     49 static struct arc4_stream rs;
     50 
     51 static inline void arc4_init(struct arc4_stream *);
     52 static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
     53 static void arc4_stir(struct arc4_stream *);
     54 static inline u_int8_t arc4_getbyte(struct arc4_stream *);
     55 static inline u_int32_t arc4_getword(struct arc4_stream *);
     56 
     57 static inline void
     58 arc4_init(as)
     59 	struct arc4_stream *as;
     60 {
     61 	int     n;
     62 
     63 	for (n = 0; n < 256; n++)
     64 		as->s[n] = n;
     65 	as->i = 0;
     66 	as->j = 0;
     67 }
     68 
     69 static inline void
     70 arc4_addrandom(as, dat, datlen)
     71 	struct arc4_stream *as;
     72 	u_char *dat;
     73 	int     datlen;
     74 {
     75 	int     n;
     76 	u_int8_t si;
     77 
     78 	as->i--;
     79 	for (n = 0; n < 256; n++) {
     80 		as->i = (as->i + 1);
     81 		si = as->s[as->i];
     82 		as->j = (as->j + si + dat[n % datlen]);
     83 		as->s[as->i] = as->s[as->j];
     84 		as->s[as->j] = si;
     85 	}
     86 	as->j = as->i;
     87 }
     88 
     89 static void
     90 arc4_stir(as)
     91 	struct arc4_stream *as;
     92 {
     93 	int     fd;
     94 	struct {
     95 		struct timeval tv;
     96 		u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)];
     97 	}       rdat;
     98 
     99 	gettimeofday(&rdat.tv, NULL);
    100 	fd = open("/dev/urandom", O_RDONLY);
    101 	if (fd != -1) {
    102 		read(fd, rdat.rnd, sizeof(rdat.rnd));
    103 		close(fd);
    104 	}
    105 #ifdef KERN_ARND
    106 	else {
    107 		int i, mib[2];
    108 		size_t len;
    109 
    110 		/* Device could not be opened, we might be chrooted, take
    111 		 * randomness from sysctl. */
    112 
    113 		mib[0] = CTL_KERN;
    114 		mib[1] = KERN_ARND;
    115 
    116 		for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i ++) {
    117 			len = sizeof(u_int);
    118 			if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1)
    119 				break;
    120 		}
    121 	}
    122 #endif
    123 	/* fd < 0 or failed sysctl ?  Ah, what the heck. We'll just take
    124 	 * whatever was on the stack... */
    125 
    126 	arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
    127 }
    128 
    129 static inline u_int8_t
    130 arc4_getbyte(as)
    131 	struct arc4_stream *as;
    132 {
    133 	u_int8_t si, sj;
    134 
    135 	as->i = (as->i + 1);
    136 	si = as->s[as->i];
    137 	as->j = (as->j + si);
    138 	sj = as->s[as->j];
    139 	as->s[as->i] = sj;
    140 	as->s[as->j] = si;
    141 	return (as->s[(si + sj) & 0xff]);
    142 }
    143 
    144 static inline u_int32_t
    145 arc4_getword(as)
    146 	struct arc4_stream *as;
    147 {
    148 	u_int32_t val;
    149 	val = arc4_getbyte(as) << 24;
    150 	val |= arc4_getbyte(as) << 16;
    151 	val |= arc4_getbyte(as) << 8;
    152 	val |= arc4_getbyte(as);
    153 	return val;
    154 }
    155 
    156 void
    157 arc4random_stir()
    158 {
    159 	if (!rs_initialized) {
    160 		arc4_init(&rs);
    161 		rs_initialized = 1;
    162 	}
    163 	arc4_stir(&rs);
    164 }
    165 
    166 void
    167 arc4random_addrandom(dat, datlen)
    168 	u_char *dat;
    169 	int     datlen;
    170 {
    171 	if (!rs_initialized)
    172 		arc4random_stir();
    173 	arc4_addrandom(&rs, dat, datlen);
    174 }
    175 
    176 u_int32_t
    177 arc4random()
    178 {
    179 	if (!rs_initialized)
    180 		arc4random_stir();
    181 	return arc4_getword(&rs);
    182 }
    183 
    184 #if 0
    185 /*-------- Test code for i386 --------*/
    186 #include <stdio.h>
    187 #include <machine/pctr.h>
    188 int
    189 main(int argc, char **argv)
    190 {
    191 	const int iter = 1000000;
    192 	int     i;
    193 	pctrval v;
    194 
    195 	v = rdtsc();
    196 	for (i = 0; i < iter; i++)
    197 		arc4random();
    198 	v = rdtsc() - v;
    199 	v /= iter;
    200 
    201 	printf("%qd cycles\n", v);
    202 }
    203 #endif
    204