xref: /OK3568_Linux_fs/buildroot/package/x11r7/mcookie/mcookie.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* mcookie.c -- Generates random numbers for xauth
2*4882a593Smuzhiyun  * Created: Fri Feb  3 10:42:48 1995 by faith@cs.unc.edu
3*4882a593Smuzhiyun  * Revised: Fri Mar 19 07:48:01 1999 by faith@acm.org
4*4882a593Smuzhiyun  * Public Domain 1995, 1999 Rickard E. Faith (faith@acm.org)
5*4882a593Smuzhiyun  * This program comes with ABSOLUTELY NO WARRANTY.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * $Id: mcookie.c,v 1.5 1997/07/06 00:13:06 aebr Exp $
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program gathers some random bits of data and used the MD5
10*4882a593Smuzhiyun  * message-digest algorithm to generate a 128-bit hexadecimal number for
11*4882a593Smuzhiyun  * use with xauth(1).
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * NOTE: Unless /dev/random is available, this program does not actually
14*4882a593Smuzhiyun  * gather 128 bits of random information, so the magic cookie generated
15*4882a593Smuzhiyun  * will be considerably easier to guess than one might expect.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * 1999-02-22 Arkadiusz Mi�kiewicz <misiek@pld.ORG.PL>
18*4882a593Smuzhiyun  * - added Native Language Support
19*4882a593Smuzhiyun  * 1999-03-21 aeb: Added some fragments of code from Colin Plumb.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <stdio.h>
24*4882a593Smuzhiyun #include <stdlib.h>
25*4882a593Smuzhiyun #include <fcntl.h>
26*4882a593Smuzhiyun #include <sys/time.h>
27*4882a593Smuzhiyun #include <time.h>
28*4882a593Smuzhiyun #include <unistd.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define BUFFERSIZE 4096
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #ifndef MD5_H
34*4882a593Smuzhiyun #define MD5_H
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #if defined (__alpha__) || defined (__ia64__) || defined (__x86_64__)
37*4882a593Smuzhiyun typedef unsigned int uint32;
38*4882a593Smuzhiyun #else
39*4882a593Smuzhiyun typedef unsigned long uint32;
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun struct MD5Context {
43*4882a593Smuzhiyun 	uint32 buf[4];
44*4882a593Smuzhiyun 	uint32 bits[2];
45*4882a593Smuzhiyun 	unsigned char in[64];
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun void MD5Init(struct MD5Context *context);
49*4882a593Smuzhiyun void MD5Update(struct MD5Context *context, unsigned char const *buf,
50*4882a593Smuzhiyun 	       unsigned len);
51*4882a593Smuzhiyun void MD5Final(unsigned char digest[16], struct MD5Context *context);
52*4882a593Smuzhiyun void MD5Transform(uint32 buf[4], uint32 const in[16]);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun  * This is needed to make RSAREF happy on some MS-DOS compilers.
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun typedef struct MD5Context MD5_CTX;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #endif /* !MD5_H */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun  * This code implements the MD5 message-digest algorithm.
65*4882a593Smuzhiyun  * The algorithm is due to Ron Rivest.  This code was
66*4882a593Smuzhiyun  * written by Colin Plumb in 1993, no copyright is claimed.
67*4882a593Smuzhiyun  * This code is in the public domain; do with it what you wish.
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * Equivalent code is available from RSA Data Security, Inc.
70*4882a593Smuzhiyun  * This code has been tested against that, and is equivalent,
71*4882a593Smuzhiyun  * except that you don't need to include two pages of legalese
72*4882a593Smuzhiyun  * with every copy.
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * To compute the message digest of a chunk of bytes, declare an
75*4882a593Smuzhiyun  * MD5Context structure, pass it to MD5Init, call MD5Update as
76*4882a593Smuzhiyun  * needed on buffers full of bytes, and then call MD5Final, which
77*4882a593Smuzhiyun  * will fill a supplied 16-byte array with the digest.
78*4882a593Smuzhiyun  */
79*4882a593Smuzhiyun #include <string.h>		/* for memcpy() */
80*4882a593Smuzhiyun #include <endian.h>
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #if __BYTE_ORDER == __LITTLE_ENDIAN
83*4882a593Smuzhiyun #define byteReverse(buf, len)	/* Nothing */
84*4882a593Smuzhiyun #else
85*4882a593Smuzhiyun void byteReverse(unsigned char *buf, unsigned longs);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun  * Note: this code is harmless on little-endian machines.
89*4882a593Smuzhiyun  */
byteReverse(unsigned char * buf,unsigned longs)90*4882a593Smuzhiyun void byteReverse(unsigned char *buf, unsigned longs)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun     uint32 t;
93*4882a593Smuzhiyun     do {
94*4882a593Smuzhiyun 	t = (uint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
95*4882a593Smuzhiyun 	    ((unsigned) buf[1] << 8 | buf[0]);
96*4882a593Smuzhiyun 	*(uint32 *) buf = t;
97*4882a593Smuzhiyun 	buf += 4;
98*4882a593Smuzhiyun     } while (--longs);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun #endif
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
104*4882a593Smuzhiyun  * initialization constants.
105*4882a593Smuzhiyun  */
MD5Init(struct MD5Context * ctx)106*4882a593Smuzhiyun void MD5Init(struct MD5Context *ctx)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun     ctx->buf[0] = 0x67452301;
109*4882a593Smuzhiyun     ctx->buf[1] = 0xefcdab89;
110*4882a593Smuzhiyun     ctx->buf[2] = 0x98badcfe;
111*4882a593Smuzhiyun     ctx->buf[3] = 0x10325476;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun     ctx->bits[0] = 0;
114*4882a593Smuzhiyun     ctx->bits[1] = 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun  * Update context to reflect the concatenation of another buffer full
119*4882a593Smuzhiyun  * of bytes.
120*4882a593Smuzhiyun  */
MD5Update(struct MD5Context * ctx,unsigned char const * buf,unsigned len)121*4882a593Smuzhiyun void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun     uint32 t;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun     /* Update bitcount */
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun     t = ctx->bits[0];
128*4882a593Smuzhiyun     if ((ctx->bits[0] = t + ((uint32) len << 3)) < t)
129*4882a593Smuzhiyun 	ctx->bits[1]++;		/* Carry from low to high */
130*4882a593Smuzhiyun     ctx->bits[1] += len >> 29;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun     t = (t >> 3) & 0x3f;	/* Bytes already in shsInfo->data */
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun     /* Handle any leading odd-sized chunks */
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun     if (t) {
137*4882a593Smuzhiyun 	unsigned char *p = (unsigned char *) ctx->in + t;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	t = 64 - t;
140*4882a593Smuzhiyun 	if (len < t) {
141*4882a593Smuzhiyun 	    memcpy(p, buf, len);
142*4882a593Smuzhiyun 	    return;
143*4882a593Smuzhiyun 	}
144*4882a593Smuzhiyun 	memcpy(p, buf, t);
145*4882a593Smuzhiyun 	byteReverse(ctx->in, 16);
146*4882a593Smuzhiyun 	MD5Transform(ctx->buf, (uint32 *) ctx->in);
147*4882a593Smuzhiyun 	buf += t;
148*4882a593Smuzhiyun 	len -= t;
149*4882a593Smuzhiyun     }
150*4882a593Smuzhiyun     /* Process data in 64-byte chunks */
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun     while (len >= 64) {
153*4882a593Smuzhiyun 	memcpy(ctx->in, buf, 64);
154*4882a593Smuzhiyun 	byteReverse(ctx->in, 16);
155*4882a593Smuzhiyun 	MD5Transform(ctx->buf, (uint32 *) ctx->in);
156*4882a593Smuzhiyun 	buf += 64;
157*4882a593Smuzhiyun 	len -= 64;
158*4882a593Smuzhiyun     }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun     /* Handle any remaining bytes of data. */
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun     memcpy(ctx->in, buf, len);
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun  * Final wrapup - pad to 64-byte boundary with the bit pattern
167*4882a593Smuzhiyun  * 1 0* (64-bit count of bits processed, MSB-first)
168*4882a593Smuzhiyun  */
MD5Final(unsigned char digest[16],struct MD5Context * ctx)169*4882a593Smuzhiyun void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun     unsigned count;
172*4882a593Smuzhiyun     unsigned char *p;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun     /* Compute number of bytes mod 64 */
175*4882a593Smuzhiyun     count = (ctx->bits[0] >> 3) & 0x3F;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun     /* Set the first char of padding to 0x80.  This is safe since there is
178*4882a593Smuzhiyun        always at least one byte free */
179*4882a593Smuzhiyun     p = ctx->in + count;
180*4882a593Smuzhiyun     *p++ = 0x80;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun     /* Bytes of padding needed to make 64 bytes */
183*4882a593Smuzhiyun     count = 64 - 1 - count;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun     /* Pad out to 56 mod 64 */
186*4882a593Smuzhiyun     if (count < 8) {
187*4882a593Smuzhiyun 	/* Two lots of padding:  Pad the first block to 64 bytes */
188*4882a593Smuzhiyun 	memset(p, 0, count);
189*4882a593Smuzhiyun 	byteReverse(ctx->in, 16);
190*4882a593Smuzhiyun 	MD5Transform(ctx->buf, (uint32 *) ctx->in);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	/* Now fill the next block with 56 bytes */
193*4882a593Smuzhiyun 	memset(ctx->in, 0, 56);
194*4882a593Smuzhiyun     } else {
195*4882a593Smuzhiyun 	/* Pad block to 56 bytes */
196*4882a593Smuzhiyun 	memset(p, 0, count - 8);
197*4882a593Smuzhiyun     }
198*4882a593Smuzhiyun     byteReverse(ctx->in, 14);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun     /* Append length in bits and transform */
201*4882a593Smuzhiyun     ((uint32 *) ctx->in)[14] = ctx->bits[0];
202*4882a593Smuzhiyun     ((uint32 *) ctx->in)[15] = ctx->bits[1];
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun     MD5Transform(ctx->buf, (uint32 *) ctx->in);
205*4882a593Smuzhiyun     byteReverse((unsigned char *) ctx->buf, 4);
206*4882a593Smuzhiyun     memcpy(digest, ctx->buf, 16);
207*4882a593Smuzhiyun     memset(ctx, 0, sizeof(*ctx));	/* In case it's sensitive */
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /* The four core functions - F1 is optimized somewhat */
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun /* #define F1(x, y, z) (x & y | ~x & z) */
213*4882a593Smuzhiyun #define F1(x, y, z) (z ^ (x & (y ^ z)))
214*4882a593Smuzhiyun #define F2(x, y, z) F1(z, x, y)
215*4882a593Smuzhiyun #define F3(x, y, z) (x ^ y ^ z)
216*4882a593Smuzhiyun #define F4(x, y, z) (y ^ (x | ~z))
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /* This is the central step in the MD5 algorithm. */
219*4882a593Smuzhiyun #define MD5STEP(f, w, x, y, z, data, s) \
220*4882a593Smuzhiyun 	( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun /*
223*4882a593Smuzhiyun  * The core of the MD5 algorithm, this alters an existing MD5 hash to
224*4882a593Smuzhiyun  * reflect the addition of 16 longwords of new data.  MD5Update blocks
225*4882a593Smuzhiyun  * the data and converts bytes into longwords for this routine.
226*4882a593Smuzhiyun  */
MD5Transform(uint32 buf[4],uint32 const in[16])227*4882a593Smuzhiyun void MD5Transform(uint32 buf[4], uint32 const in[16])
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun     register uint32 a, b, c, d;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun     a = buf[0];
232*4882a593Smuzhiyun     b = buf[1];
233*4882a593Smuzhiyun     c = buf[2];
234*4882a593Smuzhiyun     d = buf[3];
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
237*4882a593Smuzhiyun     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
238*4882a593Smuzhiyun     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
239*4882a593Smuzhiyun     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
240*4882a593Smuzhiyun     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
241*4882a593Smuzhiyun     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
242*4882a593Smuzhiyun     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
243*4882a593Smuzhiyun     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
244*4882a593Smuzhiyun     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
245*4882a593Smuzhiyun     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
246*4882a593Smuzhiyun     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
247*4882a593Smuzhiyun     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
248*4882a593Smuzhiyun     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
249*4882a593Smuzhiyun     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
250*4882a593Smuzhiyun     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
251*4882a593Smuzhiyun     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
254*4882a593Smuzhiyun     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
255*4882a593Smuzhiyun     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
256*4882a593Smuzhiyun     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
257*4882a593Smuzhiyun     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
258*4882a593Smuzhiyun     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
259*4882a593Smuzhiyun     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
260*4882a593Smuzhiyun     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
261*4882a593Smuzhiyun     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
262*4882a593Smuzhiyun     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
263*4882a593Smuzhiyun     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
264*4882a593Smuzhiyun     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
265*4882a593Smuzhiyun     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
266*4882a593Smuzhiyun     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
267*4882a593Smuzhiyun     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
268*4882a593Smuzhiyun     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
271*4882a593Smuzhiyun     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
272*4882a593Smuzhiyun     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
273*4882a593Smuzhiyun     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
274*4882a593Smuzhiyun     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
275*4882a593Smuzhiyun     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
276*4882a593Smuzhiyun     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
277*4882a593Smuzhiyun     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
278*4882a593Smuzhiyun     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
279*4882a593Smuzhiyun     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
280*4882a593Smuzhiyun     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
281*4882a593Smuzhiyun     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
282*4882a593Smuzhiyun     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
283*4882a593Smuzhiyun     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
284*4882a593Smuzhiyun     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
285*4882a593Smuzhiyun     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
288*4882a593Smuzhiyun     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
289*4882a593Smuzhiyun     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
290*4882a593Smuzhiyun     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
291*4882a593Smuzhiyun     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
292*4882a593Smuzhiyun     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
293*4882a593Smuzhiyun     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
294*4882a593Smuzhiyun     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
295*4882a593Smuzhiyun     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
296*4882a593Smuzhiyun     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
297*4882a593Smuzhiyun     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
298*4882a593Smuzhiyun     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
299*4882a593Smuzhiyun     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
300*4882a593Smuzhiyun     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
301*4882a593Smuzhiyun     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
302*4882a593Smuzhiyun     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun     buf[0] += a;
305*4882a593Smuzhiyun     buf[1] += b;
306*4882a593Smuzhiyun     buf[2] += c;
307*4882a593Smuzhiyun     buf[3] += d;
308*4882a593Smuzhiyun }
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun struct rngs {
314*4882a593Smuzhiyun    const char *path;
315*4882a593Smuzhiyun    int minlength, maxlength;
316*4882a593Smuzhiyun } rngs[] = {
317*4882a593Smuzhiyun    { "/dev/random",              16,  16 }, /* 16 bytes = 128 bits suffice */
318*4882a593Smuzhiyun    { "/proc/interrupts",          0,   0 },
319*4882a593Smuzhiyun    { "/proc/slabinfo",            0,   0 },
320*4882a593Smuzhiyun    { "/proc/stat",                0,   0 },
321*4882a593Smuzhiyun    { "/dev/urandom",             32,  64 },
322*4882a593Smuzhiyun };
323*4882a593Smuzhiyun #define RNGS (sizeof(rngs)/sizeof(struct rngs))
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun int Verbose = 0;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun /* The basic function to hash a file */
328*4882a593Smuzhiyun static off_t
hash_file(struct MD5Context * ctx,int fd)329*4882a593Smuzhiyun hash_file(struct MD5Context *ctx, int fd)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun    off_t count = 0;
332*4882a593Smuzhiyun    ssize_t r;
333*4882a593Smuzhiyun    unsigned char buf[BUFFERSIZE];
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun    while ((r = read(fd, buf, sizeof(buf))) > 0) {
336*4882a593Smuzhiyun       MD5Update(ctx, buf, r);
337*4882a593Smuzhiyun       count += r;
338*4882a593Smuzhiyun    }
339*4882a593Smuzhiyun    /* Separate files with a null byte */
340*4882a593Smuzhiyun    buf[0] = 0;
341*4882a593Smuzhiyun    MD5Update(ctx, buf, 1);
342*4882a593Smuzhiyun    return count;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
main(int argc,char ** argv)345*4882a593Smuzhiyun int main( int argc, char **argv )
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun    int               i;
348*4882a593Smuzhiyun    struct MD5Context ctx;
349*4882a593Smuzhiyun    unsigned char     digest[16];
350*4882a593Smuzhiyun    unsigned char     buf[BUFFERSIZE];
351*4882a593Smuzhiyun    int               fd;
352*4882a593Smuzhiyun    int               c;
353*4882a593Smuzhiyun    pid_t             pid;
354*4882a593Smuzhiyun    char              *file = NULL;
355*4882a593Smuzhiyun    int               r;
356*4882a593Smuzhiyun    struct timeval    tv;
357*4882a593Smuzhiyun    struct timezone   tz;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun    while ((c = getopt( argc, argv, "vf:" )) != -1)
360*4882a593Smuzhiyun       switch (c) {
361*4882a593Smuzhiyun       case 'v': ++Verbose;     break;
362*4882a593Smuzhiyun       case 'f': file = optarg; break;
363*4882a593Smuzhiyun       }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun    MD5Init( &ctx );
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun    gettimeofday( &tv, &tz );
368*4882a593Smuzhiyun    MD5Update( &ctx, (unsigned char *)&tv, sizeof( tv ) );
369*4882a593Smuzhiyun    pid = getppid();
370*4882a593Smuzhiyun    MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
371*4882a593Smuzhiyun    pid = getpid();
372*4882a593Smuzhiyun    MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun    if (file) {
375*4882a593Smuzhiyun       int count = 0;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun       if (file[0] == '-' && !file[1])
378*4882a593Smuzhiyun 	 fd = fileno(stdin);
379*4882a593Smuzhiyun       else
380*4882a593Smuzhiyun 	 fd = open( file, O_RDONLY );
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun       if (fd < 0) {
383*4882a593Smuzhiyun 	 fprintf( stderr, "Could not open %s\n", file );
384*4882a593Smuzhiyun       } else {
385*4882a593Smuzhiyun          count = hash_file( &ctx, fd );
386*4882a593Smuzhiyun 	 if (Verbose)
387*4882a593Smuzhiyun 	    fprintf( stderr, "Got %d bytes from %s\n", count, file );
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	 if (file[0] != '-' || file[1]) close( fd );
390*4882a593Smuzhiyun       }
391*4882a593Smuzhiyun    }
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun    for (i = 0; i < RNGS; i++) {
394*4882a593Smuzhiyun       if ((fd = open( rngs[i].path, O_RDONLY|O_NONBLOCK )) >= 0) {
395*4882a593Smuzhiyun 	 int count = sizeof(buf);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	 if (rngs[i].maxlength && count > rngs[i].maxlength)
398*4882a593Smuzhiyun 	    count = rngs[i].maxlength;
399*4882a593Smuzhiyun 	 r = read( fd, buf, count );
400*4882a593Smuzhiyun 	 if (r > 0)
401*4882a593Smuzhiyun 	    MD5Update( &ctx, buf, r );
402*4882a593Smuzhiyun 	 else
403*4882a593Smuzhiyun 	    r = 0;
404*4882a593Smuzhiyun 	 close( fd );
405*4882a593Smuzhiyun 	 if (Verbose)
406*4882a593Smuzhiyun 	    fprintf( stderr, "Got %d bytes from %s\n", r, rngs[i].path );
407*4882a593Smuzhiyun 	 if (rngs[i].minlength && r >= rngs[i].minlength)
408*4882a593Smuzhiyun 	    break;
409*4882a593Smuzhiyun       } else if (Verbose)
410*4882a593Smuzhiyun 	 fprintf( stderr, "Could not open %s\n", rngs[i].path );
411*4882a593Smuzhiyun    }
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun    MD5Final( digest, &ctx );
414*4882a593Smuzhiyun    for (i = 0; i < 16; i++) printf( "%02x", digest[i] );
415*4882a593Smuzhiyun    putchar ( '\n' );
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun    /*
418*4882a593Smuzhiyun     * The following is important for cases like disk full, so shell scripts
419*4882a593Smuzhiyun     * can bomb out properly rather than think they succeeded.
420*4882a593Smuzhiyun     */
421*4882a593Smuzhiyun    if (fflush(stdout) < 0 || fclose(stdout) < 0)
422*4882a593Smuzhiyun       return 1;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun    return 0;
425*4882a593Smuzhiyun }
426