mcookie.c 12 KB

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