xref: /onnv-gate/usr/src/lib/crypt_modules/bsdbf/arc4random.c (revision 420:45e68e7aede3)
10Sstevel@tonic-gate /*
2*420Sstevel  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*420Sstevel  * Use is subject to license terms.
40Sstevel@tonic-gate  */
5*420Sstevel 
6*420Sstevel #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*420Sstevel 
80Sstevel@tonic-gate /*	$OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $	*/
90Sstevel@tonic-gate 
100Sstevel@tonic-gate /*
110Sstevel@tonic-gate  * Arc4 random number generator for OpenBSD.
120Sstevel@tonic-gate  * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * Modification and redistribution in source and binary forms is
150Sstevel@tonic-gate  * permitted provided that due credit is given to the author and the
160Sstevel@tonic-gate  * OpenBSD project by leaving this copyright notice intact.
170Sstevel@tonic-gate  */
180Sstevel@tonic-gate 
190Sstevel@tonic-gate /*
200Sstevel@tonic-gate  * This code is derived from section 17.1 of Applied Cryptography,
210Sstevel@tonic-gate  * second edition, which describes a stream cipher allegedly
220Sstevel@tonic-gate  * compatible with RSA Labs "RC4" cipher (the actual description of
230Sstevel@tonic-gate  * which is a trade secret).  The same algorithm is used as a stream
240Sstevel@tonic-gate  * cipher called "arcfour" in Tatu Ylonen's ssh package.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * Here the stream cipher has been modified always to include the time
270Sstevel@tonic-gate  * when initializing the state.  That makes it impossible to
280Sstevel@tonic-gate  * regenerate the same random sequence twice, so this can't be used
290Sstevel@tonic-gate  * for encryption, but will generate good random numbers.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * RC4 is a registered trademark of RSA Laboratories.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include <sys/time.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #ifdef __GNUC__
420Sstevel@tonic-gate #define inline __inline
430Sstevel@tonic-gate #else				/* !__GNUC__ */
440Sstevel@tonic-gate #define inline
450Sstevel@tonic-gate #endif				/* !__GNUC__ */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate struct arc4_stream {
480Sstevel@tonic-gate 	uint8_t i;
490Sstevel@tonic-gate 	uint8_t j;
500Sstevel@tonic-gate 	uint8_t s[256];
510Sstevel@tonic-gate };
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int     rs_initialized;
540Sstevel@tonic-gate static struct arc4_stream rs;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static inline void
arc4_init(as)570Sstevel@tonic-gate arc4_init(as)
580Sstevel@tonic-gate 	struct arc4_stream *as;
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	int     n;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	for (n = 0; n < 256; n++)
630Sstevel@tonic-gate 		as->s[n] = n;
640Sstevel@tonic-gate 	as->i = 0;
650Sstevel@tonic-gate 	as->j = 0;
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static inline void
arc4_addrandom(as,dat,datlen)690Sstevel@tonic-gate arc4_addrandom(as, dat, datlen)
700Sstevel@tonic-gate 	struct arc4_stream *as;
710Sstevel@tonic-gate 	u_char *dat;
720Sstevel@tonic-gate 	size_t    datlen;
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	int     n;
750Sstevel@tonic-gate 	uint8_t si;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	as->i--;
780Sstevel@tonic-gate 	for (n = 0; n < 256; n++) {
790Sstevel@tonic-gate 		as->i = (as->i + 1);
800Sstevel@tonic-gate 		si = as->s[as->i];
810Sstevel@tonic-gate 		as->j = (as->j + si + dat[n % datlen]);
820Sstevel@tonic-gate 		as->s[as->i] = as->s[as->j];
830Sstevel@tonic-gate 		as->s[as->j] = si;
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	as->j = as->i;
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static void
arc4_stir(as)890Sstevel@tonic-gate arc4_stir(as)
900Sstevel@tonic-gate 	struct arc4_stream *as;
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	int     fd;
930Sstevel@tonic-gate 	struct {
940Sstevel@tonic-gate 		struct timeval tv;
950Sstevel@tonic-gate 		uint rnd[(128 - sizeof(struct timeval)) / sizeof(uint)];
960Sstevel@tonic-gate 	}       rdat;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	(void) gettimeofday(&rdat.tv, NULL);
990Sstevel@tonic-gate 	fd = open("/dev/urandom", O_RDONLY);
1000Sstevel@tonic-gate 	if (fd != -1) {
1010Sstevel@tonic-gate 		(void) read(fd, rdat.rnd, sizeof(rdat.rnd));
1020Sstevel@tonic-gate 		(void) close(fd);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	/* fd < 0 ?  Ah, what the heck. We'll just take
1050Sstevel@tonic-gate 	 * whatever was on the stack... */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static inline uint8_t
arc4_getbyte(as)1110Sstevel@tonic-gate arc4_getbyte(as)
1120Sstevel@tonic-gate 	struct arc4_stream *as;
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	uint8_t si, sj;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	as->i = (as->i + 1);
1170Sstevel@tonic-gate 	si = as->s[as->i];
1180Sstevel@tonic-gate 	as->j = (as->j + si);
1190Sstevel@tonic-gate 	sj = as->s[as->j];
1200Sstevel@tonic-gate 	as->s[as->i] = sj;
1210Sstevel@tonic-gate 	as->s[as->j] = si;
1220Sstevel@tonic-gate 	return (as->s[(si + sj) & 0xff]);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static inline uint32_t
arc4_getword(as)1260Sstevel@tonic-gate arc4_getword(as)
1270Sstevel@tonic-gate 	struct arc4_stream *as;
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	uint32_t val;
1300Sstevel@tonic-gate 	val = arc4_getbyte(as) << 24;
1310Sstevel@tonic-gate 	val |= arc4_getbyte(as) << 16;
1320Sstevel@tonic-gate 	val |= arc4_getbyte(as) << 8;
1330Sstevel@tonic-gate 	val |= arc4_getbyte(as);
1340Sstevel@tonic-gate 	return val;
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate void
arc4random_stir()1380Sstevel@tonic-gate arc4random_stir()
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	if (!rs_initialized) {
1410Sstevel@tonic-gate 		arc4_init(&rs);
1420Sstevel@tonic-gate 		rs_initialized = 1;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	arc4_stir(&rs);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate void
arc4random_addrandom(dat,datlen)1480Sstevel@tonic-gate arc4random_addrandom(dat, datlen)
1490Sstevel@tonic-gate 	u_char *dat;
1500Sstevel@tonic-gate 	size_t     datlen;
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	if (!rs_initialized)
1530Sstevel@tonic-gate 		arc4random_stir();
1540Sstevel@tonic-gate 	arc4_addrandom(&rs, dat, datlen);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate uint32_t
arc4random()1580Sstevel@tonic-gate arc4random()
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	if (!rs_initialized)
1610Sstevel@tonic-gate 		arc4random_stir();
1620Sstevel@tonic-gate 	return arc4_getword(&rs);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate #if 0
1660Sstevel@tonic-gate /*-------- Test code for i386 --------*/
1670Sstevel@tonic-gate #include <stdio.h>
1680Sstevel@tonic-gate #include <machine/pctr.h>
1690Sstevel@tonic-gate int
1700Sstevel@tonic-gate main(int argc, char **argv)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	const int iter = 1000000;
1730Sstevel@tonic-gate 	int     i;
1740Sstevel@tonic-gate 	pctrval v;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	v = rdtsc();
1770Sstevel@tonic-gate 	for (i = 0; i < iter; i++)
1780Sstevel@tonic-gate 		arc4random();
1790Sstevel@tonic-gate 	v = rdtsc() - v;
1800Sstevel@tonic-gate 	v /= iter;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	printf("%qd cycles\n", v);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate #endif
185