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.
30 #include <librb_config.h>
33 #if !defined(HAVE_OPENSSL) && !defined(HAVE_GNUTLS) && !defined(HAVE_MBEDTLS) && !defined(HAVE_ARC4RANDOM)
35 #include "arc4random.h"
38 #include <sys/resource.h>
51 static int rs_initialized
;
52 static struct arc4_stream rs
;
54 static inline void arc4_init(struct arc4_stream
*);
55 static inline void arc4_addrandom(struct arc4_stream
*, uint8_t *, int);
56 static void arc4_stir(struct arc4_stream
*);
57 static inline uint8_t arc4_getbyte(struct arc4_stream
*);
58 static inline uint32_t arc4_getword(struct arc4_stream
*);
61 arc4_init(struct arc4_stream
*as
)
65 for(n
= 0; n
< 256; n
++)
72 arc4_addrandom(struct arc4_stream
*as
, uint8_t *dat
, int datlen
)
78 for(n
= 0; n
< 256; n
++)
82 as
->j
= (as
->j
+ si
+ dat
[n
% datlen
]);
83 as
->s
[as
->i
] = as
->s
[as
->j
];
90 arc4_stir(struct arc4_stream
*as
)
98 /* XXX this doesn't support egd sources or similiar */
101 arc4_addrandom(as
, (void *)&pid
, sizeof(pid
));
103 rb_gettimeofday(&tv
, NULL
);
104 arc4_addrandom(as
, (void *)&tv
.tv_sec
, sizeof(&tv
.tv_sec
));
105 arc4_addrandom(as
, (void *)&tv
.tv_usec
, sizeof(&tv
.tv_usec
));
106 rb_gettimeofday(&tv
, NULL
);
107 arc4_addrandom(as
, (void *)&tv
.tv_usec
, sizeof(&tv
.tv_usec
));
109 #if defined(HAVE_GETRUSAGE) && RUSAGE_SELF
112 getrusage(RUSAGE_SELF
, &buf
);
113 arc4_addrandom(as
, (void *)&buf
, sizeof(buf
));
114 memset(&buf
, 0, sizeof(buf
))}
121 fd
= open("/dev/urandom", O_RDONLY
);
124 read(fd
, rnd
, sizeof(rnd
));
126 arc4_addrandom(as
, (void *)rnd
, sizeof(rnd
));
127 memset(&rnd
, 0, sizeof(rnd
));
133 LARGE_INTEGER performanceCount
;
134 if(QueryPerformanceCounter(&performanceCount
))
136 arc4_addrandom(as
, (void *)&performanceCount
, sizeof(performanceCount
));
138 lib
= LoadLibrary("ADVAPI32.DLL");
142 BOOLEAN(APIENTRY
* pfn
) (void *, ULONG
) =
143 (BOOLEAN(APIENTRY
*) (void *, ULONG
))GetProcAddress(lib
,
144 "SystemFunction036");
147 if(pfn(rnd
, sizeof(rnd
)) == TRUE
)
148 arc4_addrandom(as
, (void *)rnd
, sizeof(rnd
));
149 memset(&rnd
, 0, sizeof(rnd
));
157 * Throw away the first N words of output, as suggested in the
158 * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
159 * by Fluher, Mantin, and Shamir.
160 * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
161 * N = 256 in our case.
163 for(n
= 0; n
< 256 * 4; n
++)
167 static inline uint8_t
168 arc4_getbyte(struct arc4_stream
*as
)
174 as
->j
= (as
->j
+ si
);
178 return (as
->s
[(si
+ sj
) & 0xff]);
181 static inline uint32_t
182 arc4_getword(struct arc4_stream
*as
)
185 val
= arc4_getbyte(as
) << 24;
186 val
|= arc4_getbyte(as
) << 16;
187 val
|= arc4_getbyte(as
) << 8;
188 val
|= arc4_getbyte(as
);
193 arc4random_stir(void)
204 arc4random_addrandom(uint8_t *dat
, int datlen
)
208 arc4_addrandom(&rs
, dat
, datlen
);
216 return arc4_getword(&rs
);