home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume19 / md4tools / part01 next >
Encoding:
Internet Message Format  |  1991-05-08  |  53.4 KB

  1. From: kent@sparky.imd.sterling.com (Kent Landfield)
  2. Newsgroups: comp.sources.misc
  3. Subject: v19i017:  md4tools - MD4 netnews verification tools, Part01/02
  4. Message-ID: <1991May9.021044.28164@sparky.IMD.Sterling.COM>
  5. Date: 9 May 91 02:10:44 GMT
  6. Approved: kent@sparky.imd.sterling.com
  7.  
  8. Submitted-by: Kent Landfield <kent@sparky.imd.sterling.com>
  9. Posting-number: Volume 19, Issue 17
  10. Archive-name: md4tools/part01
  11.  
  12. MD4 can be used to apply a fingerprint on an article posted to USENET.  
  13. When run through a verification tool, MD4 will tell you whether an article 
  14. has been corrupted.  The use of MD4 does not detect or prevent the complete 
  15. replacement of an article.  Think of MD4 as a super-strong checksum.  The 
  16. header X-Md4-Signature: contains the value that will be checked against to 
  17. determine if the article is intact.
  18.  
  19. Starting with this posting, I am going to be using the X-Md4-Signature: on 
  20. all articles posted to comp.sources.misc. While I don't think that this is 
  21. worth doing for most general USENET articles, I think it will be extremely
  22. useful for archives.  The X-Md4-Signature: header is going to replace the 
  23. X-Checksum-Snefru: header previously used in this newsgroup.  
  24.  
  25. I would like to thank Ron Rivest (the author of RFC1186, "The MD4 Message 
  26. Digest Algorithm") for the MD4 code and RSA Data Security, Inc. for giving 
  27. me the permission to post it.  I would also like to thank Rich Salz for the 
  28. push to do it and for his snefru code that I hacked...
  29.  
  30. -Kent+
  31. ---
  32. #! /bin/sh
  33. # into a shell via "sh file" or similar.  To overwrite existing files,
  34. # type "sh file -c".
  35. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  36. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  37. # Contents:  README foo md4.c md4_def.h rfc1186
  38. # Wrapped by kent@sparky on Tue May  7 23:23:53 1991
  39. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  40. echo If this archive is complete, you will see the following message:
  41. echo '          "shar: End of archive 1 (of 2)."'
  42. if test -f 'README' -a "${1}" != "-c" ; then 
  43.   echo shar: Will not clobber existing file \"'README'\"
  44. else
  45.   echo shar: Extracting \"'README'\" \(3116 characters\)
  46.   sed "s/^X//" >'README' <<'END_OF_FILE'
  47. XAs stated in RFC1186:
  48. X   This  note describes the MD4 message digest algorithm.  The algorithm
  49. X   takes as input an input message of arbitrary length and produces as
  50. X   output a 128-bit "fingerprint" or "message digest" of the input.  It
  51. X   is conjectured that it is computationally infeasible to produce two
  52. X   messages having the same message digest, or to produce any message
  53. X   having a given prespecified target message digest.  The MD4 algorithm
  54. X   is thus ideal for digital signature applications, where a large file
  55. X   must be "compressed" in a secure manner before being signed with the
  56. X   RSA public-key cryptosystem.
  57. X
  58. XMD4 can be used to apply a fingerprint on an article posted to USENET that,
  59. Xwhen run through a verification tool, will tell you whether an article has 
  60. Xbeen corrupted. It does not detect or prevent complete replacement of the
  61. Xarticle. Think of MD4 as a super-strong checksum.
  62. X
  63. XThis package is an "assembled" set of tools that uses the MD4 Message 
  64. XDigest Algorithm specified in RFC1186.  There are three parts to the package. 
  65. X
  66. Xmd4 - This is the heart of the tools. The code was taken from RFC1186, 
  67. X      "The MD4 Message Digest Algorithm" authored by Ronald L. Rivest.
  68. X
  69. XI *completely* and shamefully stole the MD4 code from the RFC and as such it 
  70. Xmust be distributed under the terms specified in md4.c, md4.h and md4driver.c.
  71. XThanks to Ron for the code and to RSA Data Security, Inc. for giving me the
  72. Xpermission to post it.
  73. X
  74. Xhashmd4 -  This program is used to apply an MD4 digest on a specified
  75. X           USENET article.  A new header is added to the article. The
  76. X           header X-Md4-Signature: contains the value that will be checked
  77. X           against to determine if the article is intact.
  78. X
  79. Xcheckmd4 - This is the program used to check whether the MD4 digest
  80. X           of a USENET article indicates whether or not the article
  81. X           has been tampered with.
  82. X
  83. XI would like to thank Rich Salz for posting snefru and giving me something
  84. Xto "hack".. :-) checkmd4 and hashmd4 look almost exactly like Rich's code
  85. Xfor good reason. They are... I did the bare minimum to get this working.
  86. XThat's all I had to do... :-)  For those of you currently using hashnews
  87. Xand checknews, this (other than the names) should be a plug amd play...
  88. X
  89. XThere are manual pages for checkmd4 and for hashmd4 supplied.  The actual 
  90. XRFC1186 is supplied as a reference for MD4.
  91. X
  92. XCode that does not contain an explicit RSA copyright was derived from
  93. XRich Salz's code that was already in the public domain. Feel free to do 
  94. Xwhat you want with those pieces.
  95. X
  96. XThis code needs a 32bit machine; good luck if you've only got 16 bits!
  97. X
  98. XTo compile, edit the Makefile if you use strchr/strrchr rather than
  99. Xindex/rindex.  Also edit md4.c to determine the byte order of your
  100. Xmachine.  There is also a discussion of how to use the routines in
  101. Xmd4.c in your own programs.
  102. X
  103. XStarting with this posting, I am going to be using hashmd4 on all my
  104. Xc.s.m articles.  While I don't think that this is worth doing for most
  105. Xgeneral USENET articles, I think it will be an interesting experiment for
  106. Xarchives. 
  107. X
  108. XEnjoy!
  109. X        -Kent+
  110. END_OF_FILE
  111.   if test 3116 -ne `wc -c <'README'`; then
  112.     echo shar: \"'README'\" unpacked with wrong size!
  113.   fi
  114.   # end of 'README'
  115. fi
  116. if test -f 'foo' -a "${1}" != "-c" ; then 
  117.   echo shar: Will not clobber existing file \"'foo'\"
  118. else
  119.   echo shar: Extracting \"'foo'\" \(4 characters\)
  120.   sed "s/^X//" >'foo' <<'END_OF_FILE'
  121. Xabc
  122. END_OF_FILE
  123.   if test 4 -ne `wc -c <'foo'`; then
  124.     echo shar: \"'foo'\" unpacked with wrong size!
  125.   fi
  126.   # end of 'foo'
  127. fi
  128. if test -f 'md4.c' -a "${1}" != "-c" ; then 
  129.   echo shar: Will not clobber existing file \"'md4.c'\"
  130. else
  131.   echo shar: Extracting \"'md4.c'\" \(10725 characters\)
  132.   sed "s/^X//" >'md4.c' <<'END_OF_FILE'
  133. X/*
  134. X*--------------------------------------------------------------------------*
  135. X* (C) Copyright 1990, RSA Data Security, Inc.  All rights reserved.        *
  136. X* License to copy and use this software is granted provided it is          *
  137. X* identified as the "RSA Data Security, Inc. MD4 message digest algorithm" *
  138. X* in all material mentioning or referencing this software or function.     *
  139. X*                                                                          *
  140. X* License is also granted to make and use derivative works provided such   *
  141. X* works are identified as "derived from the RSA Data Securitry, Inc. MD4   *
  142. X* message digest algorithm" in all material mentioning or referencing the  *
  143. X* derived work.                                                            *
  144. X*                                                                          *
  145. X* RSA Data Security, Inc. makes no representations concerning the          *
  146. X* merchantability of this software or the suitability of the software      *
  147. X* for any particular purpose.  It is provided "as is" without express      *
  148. X* or implied warranty of any kind.                                         *
  149. X*                                                                          *
  150. X* These notices must be retained in any copies of any part of this         *
  151. X* documentation and/or software.                                           *
  152. X*--------------------------------------------------------------------------*
  153. X** ********************************************************************
  154. X** md4.c -- Implementation of MD4 Message Digest Algorithm           **
  155. X** Updated: 2/16/90 by Ronald L. Rivest                              **
  156. X** (C) 1990 RSA Data Security, Inc.                                  **
  157. X** ********************************************************************
  158. X*/
  159. X
  160. X/*
  161. X** To use MD4:
  162. X**   -- Include md4.h in your program
  163. X**   -- Declare an MDstruct MD to hold the state of the digest
  164. X**          computation.
  165. X**   -- Initialize MD using MDbegin(&MD)
  166. X**   -- For each full block (64 bytes) X you wish to process, call
  167. X**          MDupdate(&MD,X,512)
  168. X**      (512 is the number of bits in a full block.)
  169. X**   -- For the last block (less than 64 bytes) you wish to process,
  170. X**          MDupdate(&MD,X,n)
  171. X**      where n is the number of bits in the partial block. A partial
  172. X**      block terminates the computation, so every MD computation
  173. X**      should terminate by processing a partial block, even if it
  174. X**      has n = 0.
  175. X**   -- The message digest is available in MD.buffer[0] ...
  176. X**      MD.buffer[3].  (Least-significant byte of each word
  177. X**      should be output first.)
  178. X**   -- You can print out the digest using MDprint(&MD)
  179. X*/
  180. X
  181. X/* Implementation notes:
  182. X** This implementation assumes that ints are 32-bit quantities.
  183. X** If the machine stores the least-significant byte of an int in the
  184. X** least-addressed byte (e.g., VAX and 8086), then LOWBYTEFIRST
  185. X** should be set to TRUE.  Otherwise (e.g., SUNS), LOWBYTEFIRST
  186. X** should be set to FALSE.  Note that on machines with LOWBYTEFIRST
  187. X** FALSE the routine MDupdate modifies has a side-effect on its input
  188. X** array (the order of bytes in each word are reversed).  If this is
  189. X** undesired a call to MDreverse(X) can reverse the bytes of X back
  190. X** into order after each call to MDupdate.
  191. X*/
  192. X
  193. X/* Compile-time includes
  194. X*/
  195. X#include <stdio.h>
  196. X#include "md4.h"
  197. X
  198. X#define TRUE  1
  199. X#define FALSE 0
  200. X#define LOWBYTEFIRST FALSE
  201. X
  202. X
  203. X/* Compile-time declarations of MD4 "magic constants".
  204. X*/
  205. X#define I0  0x67452301       /* Initial values for MD buffer */
  206. X#define I1  0xefcdab89
  207. X#define I2  0x98badcfe
  208. X#define I3  0x10325476
  209. X#define C2  013240474631     /* round 2 constant = sqrt(2) in octal */
  210. X#define C3  015666365641     /* round 3 constant = sqrt(3) in octal */
  211. X/* C2 and C3 are from Knuth, The Art of Programming, Volume 2
  212. X** (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
  213. X** Table 2, page 660.
  214. X*/
  215. X#define fs1  3               /* round 1 shift amounts */
  216. X#define fs2  7
  217. X#define fs3 11
  218. X#define fs4 19
  219. X#define gs1  3               /* round 2 shift amounts */
  220. X#define gs2  5
  221. X#define gs3  9
  222. X#define gs4 13
  223. X#define hs1  3               /* round 3 shift amounts */
  224. X#define hs2  9
  225. X#define hs3 11
  226. X#define hs4 15
  227. X
  228. X/* Compile-time macro declarations for MD4.
  229. X** Note: The "rot" operator uses the variable "tmp".
  230. X** It assumes tmp is declared as unsigned int, so that the >>
  231. X** operator will shift in zeros rather than extending the sign bit.
  232. X*/
  233. X#define f(X,Y,Z)             ((X&Y) | ((~X)&Z))
  234. X#define g(X,Y,Z)             ((X&Y) | (X&Z) | (Y&Z))
  235. X#define h(X,Y,Z)             (X^Y^Z)
  236. X#define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
  237. X#define ff(A,B,C,D,i,s)      A = rot((A + f(B,C,D) + X[i]),s)
  238. X#define gg(A,B,C,D,i,s)      A = rot((A + g(B,C,D) + X[i] + C2),s)
  239. X#define hh(A,B,C,D,i,s)      A = rot((A + h(B,C,D) + X[i] + C3),s)
  240. X
  241. X/* MDprint(MDp)
  242. X** Print message digest buffer MDp as 32 hexadecimal digits.
  243. X** Order is from low-order byte of buffer[0] to high-order byte of
  244. X** buffer[3].
  245. X** Each byte is printed with high-order hexadecimal digit first.
  246. X** This is a user-callable routine.
  247. X*/
  248. Xvoid
  249. XMDprint(MDp)
  250. XMDptr MDp;
  251. X{ int i,j;
  252. X  for (i=0;i<4;i++)
  253. X    for (j=0;j<32;j=j+8)
  254. X      printf("%02x",(MDp->buffer[i]>>j) & 0xFF);
  255. X}
  256. X
  257. X/* MDbegin(MDp)
  258. X** Initialize message digest buffer MDp.
  259. X** This is a user-callable routine.
  260. X*/
  261. Xvoid
  262. XMDbegin(MDp)
  263. XMDptr MDp;
  264. X{ int i;
  265. X  MDp->buffer[0] = I0;
  266. X  MDp->buffer[1] = I1;
  267. X  MDp->buffer[2] = I2;
  268. X  MDp->buffer[3] = I3;
  269. X  for (i=0;i<8;i++) MDp->count[i] = 0;
  270. X  MDp->done = 0;
  271. X}
  272. X
  273. X/* MDreverse(X)
  274. X** Reverse the byte-ordering of every int in X.
  275. X** Assumes X is an array of 16 ints.
  276. X** The macro revx reverses the byte-ordering of the next word of X.
  277. X*/
  278. X#define revx { t = (*X << 16) | (*X >> 16); \
  279. X     *X++ = ((t & 0xFF00FF00) >> 8) | ((t & 0x00FF00FF) << 8); }
  280. XMDreverse(X)
  281. Xunsigned int *X;
  282. X{ register unsigned int t;
  283. X  revx; revx; revx; revx; revx; revx; revx; revx;
  284. X  revx; revx; revx; revx; revx; revx; revx; revx;
  285. X}
  286. X
  287. X/* MDblock(MDp,X)
  288. X** Update message digest buffer MDp->buffer using 16-word data block X.
  289. X** Assumes all 16 words of X are full of data.
  290. X** Does not update MDp->count.
  291. X** This routine is not user-callable.
  292. X*/
  293. Xstatic void
  294. XMDblock(MDp,X)
  295. XMDptr MDp;
  296. Xunsigned int *X;
  297. X{
  298. X  register unsigned int tmp, A, B, C, D;
  299. X#if LOWBYTEFIRST == FALSE
  300. X  MDreverse(X);
  301. X#endif
  302. X  A = MDp->buffer[0];
  303. X  B = MDp->buffer[1];
  304. X  C = MDp->buffer[2];
  305. X  D = MDp->buffer[3];
  306. X  /* Update the message digest buffer */
  307. X  ff(A , B , C , D ,  0 , fs1); /* Round 1 */
  308. X  ff(D , A , B , C ,  1 , fs2);
  309. X  ff(C , D , A , B ,  2 , fs3);
  310. X  ff(B , C , D , A ,  3 , fs4);
  311. X  ff(A , B , C , D ,  4 , fs1);
  312. X  ff(D , A , B , C ,  5 , fs2);
  313. X  ff(C , D , A , B ,  6 , fs3);
  314. X  ff(B , C , D , A ,  7 , fs4);
  315. X  ff(A , B , C , D ,  8 , fs1);
  316. X  ff(D , A , B , C ,  9 , fs2);
  317. X  ff(C , D , A , B , 10 , fs3);
  318. X  ff(B , C , D , A , 11 , fs4);
  319. X  ff(A , B , C , D , 12 , fs1);
  320. X  ff(D , A , B , C , 13 , fs2);
  321. X  ff(C , D , A , B , 14 , fs3);
  322. X  ff(B , C , D , A , 15 , fs4);
  323. X  gg(A , B , C , D ,  0 , gs1); /* Round 2 */
  324. X  gg(D , A , B , C ,  4 , gs2);
  325. X  gg(C , D , A , B ,  8 , gs3);
  326. X  gg(B , C , D , A , 12 , gs4);
  327. X  gg(A , B , C , D ,  1 , gs1);
  328. X  gg(D , A , B , C ,  5 , gs2);
  329. X  gg(C , D , A , B ,  9 , gs3);
  330. X  gg(B , C , D , A , 13 , gs4);
  331. X  gg(A , B , C , D ,  2 , gs1);
  332. X  gg(D , A , B , C ,  6 , gs2);
  333. X  gg(C , D , A , B , 10 , gs3);
  334. X  gg(B , C , D , A , 14 , gs4);
  335. X  gg(A , B , C , D ,  3 , gs1);
  336. X  gg(D , A , B , C ,  7 , gs2);
  337. X  gg(C , D , A , B , 11 , gs3);
  338. X  gg(B , C , D , A , 15 , gs4);
  339. X  hh(A , B , C , D ,  0 , hs1); /* Round 3 */
  340. X  hh(D , A , B , C ,  8 , hs2);
  341. X  hh(C , D , A , B ,  4 , hs3);
  342. X  hh(B , C , D , A , 12 , hs4);
  343. X  hh(A , B , C , D ,  2 , hs1);
  344. X  hh(D , A , B , C , 10 , hs2);
  345. X  hh(C , D , A , B ,  6 , hs3);
  346. X  hh(B , C , D , A , 14 , hs4);
  347. X  hh(A , B , C , D ,  1 , hs1);
  348. X  hh(D , A , B , C ,  9 , hs2);
  349. X  hh(C , D , A , B ,  5 , hs3);
  350. X  hh(B , C , D , A , 13 , hs4);
  351. X  hh(A , B , C , D ,  3 , hs1);
  352. X  hh(D , A , B , C , 11 , hs2);
  353. X  hh(C , D , A , B ,  7 , hs3);
  354. X  hh(B , C , D , A , 15 , hs4);
  355. X  MDp->buffer[0] += A;
  356. X  MDp->buffer[1] += B;
  357. X  MDp->buffer[2] += C;
  358. X  MDp->buffer[3] += D;
  359. X}
  360. X
  361. X/* MDupdate(MDp,X,count)
  362. X** Input: MDp -- an MDptr
  363. X**        X -- a pointer to an array of unsigned characters.
  364. X**        count -- the number of bits of X to use.
  365. X**          (if not a multiple of 8, uses high bits of last byte.)
  366. X** Update MDp using the number of bits of X given by count.
  367. X** This is the basic input routine for an MD4 user.
  368. X** The routine completes the MD computation when count < 512, so
  369. X** every MD computation should end with one call to MDupdate with a
  370. X** count less than 512.  A call with count 0 will be ignored if the
  371. X** MD has already been terminated (done != 0), so an extra call with
  372. X** count 0 can be given as a "courtesy close" to force termination
  373. X** if desired.
  374. X*/
  375. Xvoid
  376. XMDupdate(MDp,X,count)
  377. XMDptr MDp;
  378. Xunsigned char *X;
  379. Xunsigned int count;
  380. X{ unsigned int i, tmp, bit, byte, mask;
  381. X  unsigned char XX[64];
  382. X  unsigned char *p;
  383. X  /* return with no error if this is a courtesy close with count
  384. X  ** zero and MDp->done is true.
  385. X  */
  386. X  if (count == 0 && MDp->done) return;
  387. X  /* check to see if MD is already done and report error */
  388. X  if (MDp->done)
  389. X         { printf("\nError: MDupdate MD already done."); return; }
  390. X  /* Add count to MDp->count */
  391. X  tmp = count;
  392. X  p = MDp->count;
  393. X  while (tmp)
  394. X    { tmp += *p;
  395. X      *p++ = tmp;
  396. X      tmp = tmp >> 8;
  397. X    }
  398. X  /* Process data */
  399. X  if (count == 512)
  400. X    { /* Full block of data to handle */
  401. X      MDblock(MDp,(unsigned int *)X);
  402. X    }
  403. X  else if (count > 512) /* Check for count too large */
  404. X    { printf("\nError: MDupdate called with illegal count value %d."
  405. X             ,count);
  406. X      return;
  407. X    }
  408. X  else /* partial block -- must be last block so finish up */
  409. X    { /* Find out how many bytes and residual bits there are */
  410. X      byte = count >> 3;
  411. X      bit =  count & 7;
  412. X      /* Copy X into XX since we need to modify it */
  413. X      for (i=0;i<=byte;i++)   XX[i] = X[i];
  414. X      for (i=byte+1;i<64;i++) XX[i] = 0;
  415. X      /* Add padding '1' bit and low-order zeros in last byte */
  416. X      mask = 1 << (7 - bit);
  417. X      XX[byte] = (XX[byte] | mask) & ~( mask - 1);
  418. X      /* If room for bit count, finish up with this block */
  419. X      if (byte <= 55)
  420. X        { for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
  421. X          MDblock(MDp,(unsigned int *)XX);
  422. X        }
  423. X      else /* need to do two blocks to finish up */
  424. X        { MDblock(MDp,(unsigned int *)XX);
  425. X          for (i=0;i<56;i++) XX[i] = 0;
  426. X          for (i=0;i<8;i++)  XX[56+i] = MDp->count[i];
  427. X          MDblock(MDp,(unsigned int *)XX);
  428. X        }
  429. X      /* Set flag saying we're done with MD computation */
  430. X      MDp->done = 1;
  431. X    }
  432. X}
  433. X
  434. X/*
  435. X** End of md4.c
  436. X*/
  437. END_OF_FILE
  438.   if test 10725 -ne `wc -c <'md4.c'`; then
  439.     echo shar: \"'md4.c'\" unpacked with wrong size!
  440.   fi
  441.   # end of 'md4.c'
  442. fi
  443. if test -f 'md4_def.h' -a "${1}" != "-c" ; then 
  444.   echo shar: Will not clobber existing file \"'md4_def.h'\"
  445. else
  446.   echo shar: Extracting \"'md4_def.h'\" \(194 characters\)
  447.   sed "s/^X//" >'md4_def.h' <<'END_OF_FILE'
  448. X/*
  449. X** Include file for hashmd4/checkmd4
  450. X*/
  451. X
  452. X#define CHECKSUMHDR     "X-Md4-Signature"
  453. X#define HDRFIRSTCHAR    'X'
  454. X#define TRUE            1
  455. X#define FALSE           0
  456. X#define HDRTEXTSIZE     32
  457. X
  458. END_OF_FILE
  459.   if test 194 -ne `wc -c <'md4_def.h'`; then
  460.     echo shar: \"'md4_def.h'\" unpacked with wrong size!
  461.   fi
  462.   # end of 'md4_def.h'
  463. fi
  464. if test -f 'rfc1186' -a "${1}" != "-c" ; then 
  465.   echo shar: Will not clobber existing file \"'rfc1186'\"
  466. else
  467.   echo shar: Extracting \"'rfc1186'\" \(34579 characters\)
  468.   sed "s/^X//" >'rfc1186' <<'END_OF_FILE'
  469. XNetwork Working Group                                         R. Rivest
  470. XRequest for Comments: 1186          MIT Laboratory for Computer Science
  471. X                                                           October 1990
  472. X                    The MD4 Message Digest Algorithm
  473. XStatus of this Memo
  474. X   This RFC is the specification of the MD4 Digest Algorithm.  If you
  475. X   are going to implement MD4, it is suggested you do it this way.  This
  476. X   memo is for informational use and does not constitute a standard.
  477. X   Distribution of this memo is unlimited.
  478. XTable of Contents
  479. X   1.  Abstract ....................................................    1
  480. X   2.  Terminology and Notation ....................................    2
  481. X   3.  MD4 Algorithm Description ...................................    2
  482. X   4.  Extensions ..................................................    6
  483. X   5.  Summary .....................................................    7
  484. X   6.  Acknowledgements ............................................    7
  485. X   APPENDIX - Reference Implementation .............................    7
  486. X   Security Considerations..........................................   18
  487. X   Author's Address.................................................   18
  488. X1. Abstract
  489. X   This note describes the MD4 message digest algorithm.  The algorithm
  490. X   takes as input an input message of arbitrary length and produces as
  491. X   output a 128-bit "fingerprint" or "message digest" of the input.  It
  492. X   is conjectured that it is computationally infeasible to produce two
  493. X   messages having the same message digest, or to produce any message
  494. X   having a given prespecified target message digest.  The MD4 algorithm
  495. X   is thus ideal for digital signature applications, where a large file
  496. X   must be "compressed" in a secure manner before being signed with the
  497. X   RSA public-key cryptosystem.
  498. X   The MD4 algorithm is designed to be quite fast on 32-bit machines.
  499. X   On a SUN Sparc station, MD4 runs at 1,450,000 bytes/second.  On a DEC
  500. X   MicroVax II, MD4 runs at approximately 70,000 bytes/second.  On a
  501. X   20MHz 80286, MD4 runs at approximately 32,000 bytes/second.  In
  502. X   addition, the MD4 algorithm does not require any large substitution
  503. X   tables; the algorithm can be coded quite compactly.
  504. X   The MD4 algorithm is being placed in the public domain for review and
  505. X   possible adoption as a standard.
  506. XRivest                                                          [Page 1]
  507. X
  508. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  509. X   (Note: The document supersedes an earlier draft.  The algorithm
  510. X   described here is a slight modification of the one described in the
  511. X   draft.)
  512. X2.  Terminology and Notation
  513. X   In this note a "word" is a 32-bit quantity and a byte is an 8-bit
  514. X   quantity.  A sequence of bits can be interpreted in a natural manner
  515. X   as a sequence of bytes, where each consecutive group of 8 bits is
  516. X   interpreted as a byte with the high-order (most significant) bit of
  517. X   each byte listed first.  Similarly, a sequence of bytes can be
  518. X   interpreted as a sequence of 32-bit words, where each consecutive
  519. X   group of 4 bytes is interpreted as a word with the low-order (least
  520. X   significant) byte given first.
  521. X   Let x_i denote "x sub i".  If the subscript is an expression, we
  522. X   surround it in braces, as in x_{i+1}.  Similarly, we use ^ for
  523. X   superscripts (exponentiation), so that x^i denotes x to the i-th
  524. X   power.
  525. X   Let the symbol "+" denote addition of words (i.e., modulo- 2^32
  526. X   addition). Let X <<< s denote the 32-bit value obtained by circularly
  527. X   shifting (rotating) X left by s bit positions.  Let not(X) denote the
  528. X   bit-wise complement of X, and let X v Y denote the bit-wise OR of X
  529. X   and Y.  Let X xor Y denote the bit-wise XOR of X and Y, and let XY
  530. X   denote the bit-wise AND of X and Y.
  531. X3.  MD4 Algorithm Description
  532. X   We begin by supposing that we have a b-bit message as input, and that
  533. X   we wish to find its message digest.  Here b is an arbitrary
  534. X   nonnegative integer; b may be zero, it need not be a multiple of 8,
  535. X   and it may be arbitrarily large.  We imagine the bits of the message
  536. X   written down as follows:
  537. X                m_0 m_1 ... m_{b-1} .
  538. X   The following five steps are performed to compute the message digest
  539. X   of the message.
  540. X      Step 1. Append padding bits
  541. X         The message is "padded" (extended) so that its length (in bits)
  542. X         is congruent to 448, modulo 512.  That is, the message is
  543. X         extended so that it is just 64 bits shy of being a multiple of
  544. X         512 bits long.  Padding is always performed, even if the length
  545. X         of the message is already congruent to 448, modulo 512 (in
  546. X         which case 512 bits of padding are added).
  547. XRivest                                                          [Page 2]
  548. X
  549. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  550. X         Padding is performed as follows: a single "1" bit is appended
  551. X         to the message, and then enough zero bits are appended so that
  552. X         the length in bits of the padded message becomes congruent to
  553. X         448, modulo 512.
  554. X      Step 2. Append length
  555. X         A 64-bit representation of b (the length of the message before
  556. X         the padding bits were added) is appended to the result of the
  557. X         previous step.  In the unlikely event that b is greater than
  558. X         2^64, then only the low-order 64 bits of b are used.  (These
  559. X         bits are appended as two 32-bit words and appended low-order
  560. X         word first in accordance with the previous conventions.)
  561. X         At this point the resulting message (after padding with bits
  562. X         and with b) has a length that is an exact multiple of 512 bits.
  563. X         Equivalently, this message has a length that is an exact
  564. X         multiple of 16 (32-bit) words.  Let M[0 ... N-1] denote the
  565. X         words of the resulting message, where N is a multiple of 16.
  566. X      Step 3. Initialize MD buffer
  567. X         A 4-word buffer (A,B,C,D) is used to compute the message
  568. X         digest.  Here each of A,B,C,D are 32-bit registers.  These
  569. X         registers are initialized to the following values in
  570. X         hexadecimal, low-order bytes first):
  571. X            word A:    01 23 45 67
  572. X            word B:    89 ab cd ef
  573. X            word C:    fe dc ba 98
  574. X            word D:    76 54 32 10
  575. X      Step 4. Process message in 16-word blocks
  576. X         We first define three auxiliary functions that each take
  577. X         as input three 32-bit words and produce as output one
  578. X         32-bit word.
  579. X            f(X,Y,Z)  =  XY v not(X)Z
  580. X            g(X,Y,Z)  =  XY v XZ v YZ
  581. X            h(X,Y,Z)  =  X xor Y xor Z
  582. X         In each bit position f acts as a conditional: if x then y else
  583. X         z.  (The function f could have been defined using + instead of
  584. X         v since XY and not(X)Z will never have 1's in the same bit
  585. X         position.)  In each bit position g acts as a majority function:
  586. X         if at least two of x, y, z are on, then g has a one in that bit
  587. X         position, else g has a zero. It is interesting to note that if
  588. XRivest                                                          [Page 3]
  589. X
  590. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  591. X         the bits of X, Y, and Z are independent and unbiased, the each
  592. X         bit of f(X,Y,Z) will be independent and unbiased, and similarly
  593. X         each bit of g(X,Y,Z) will be independent and unbiased.  The
  594. X         function h is the bit-wise "xor" or "parity" function; it has
  595. X         properties similar to those of f and g.
  596. X         Do the following:
  597. X         For i = 0 to N/16-1 do  /* process each 16-word block */
  598. X                 For j = 0 to 15 do: /* copy block i into X */
  599. X                   Set X[j] to M[i*16+j].
  600. X                 end /* of loop on j */
  601. X                 Save A as AA, B as BB, C as CC, and D as DD.
  602. X                 [Round 1]
  603. X                   Let [A B C D i s] denote the operation
  604. X                         A = (A + f(B,C,D) + X[i]) <<< s  .
  605. X                   Do the following 16 operations:
  606. X                         [A B C D 0 3]
  607. X                         [D A B C 1 7]
  608. X                         [C D A B 2 11]
  609. X                         [B C D A 3 19]
  610. X                         [A B C D 4 3]
  611. X                         [D A B C 5 7]
  612. X                         [C D A B 6 11]
  613. X                         [B C D A 7 19]
  614. X                         [A B C D 8 3]
  615. X                         [D A B C 9 7]
  616. X                         [C D A B 10 11]
  617. X                         [B C D A 11 19]
  618. X                         [A B C D 12 3]
  619. X                         [D A B C 13 7]
  620. X                         [C D A B 14 11]
  621. X                         [B C D A 15 19]
  622. X                 [Round 2]
  623. X                   Let [A B C D i s] denote the operation
  624. X                         A = (A + g(B,C,D) + X[i] + 5A827999) <<< s .
  625. X                   (The value 5A..99 is a hexadecimal 32-bit
  626. X                   constant, written with the high-order digit
  627. X                   first. This constant represents the square
  628. X                   root of 2.  The octal value of this constant
  629. X                   is 013240474631.  See Knuth, The Art of
  630. X                   Programming, Volume 2 (Seminumerical
  631. X                   Algorithms), Second Edition (1981),
  632. X                   Addison-Wesley.  Table 2, page 660.)
  633. X                   Do the following 16 operations:
  634. X                         [A B C D 0  3]
  635. XRivest                                                          [Page 4]
  636. X
  637. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  638. X                         [D A B C 4  5]
  639. X                         [C D A B 8  9]
  640. X                         [B C D A 12 13]
  641. X                         [A B C D 1  3]
  642. X                         [D A B C 5  5]
  643. X                         [C D A B 9  9]
  644. X                         [B C D A 13 13]
  645. X                         [A B C D 2  3]
  646. X                         [D A B C 6  5]
  647. X                         [C D A B 10 9]
  648. X                         [B C D A 14 13]
  649. X                         [A B C D 3  3]
  650. X                         [D A B C 7  5]
  651. X                         [C D A B 11 9]
  652. X                         [B C D A 15 13]
  653. X                 [Round 3]
  654. X                   Let [A B C D i s] denote the operation
  655. X                         A = (A + h(B,C,D) + X[i] + 6ED9EBA1) <<< s .
  656. X                   (The value 6E..A1 is a hexadecimal 32-bit
  657. X                   constant, written with the high-order digit
  658. X                   first.  This constant represents the square
  659. X                   root of 3.  The octal value of this constant
  660. X                   is 015666365641.  See Knuth, The Art of
  661. X                   Programming, Volume 2 (Seminumerical
  662. X                   Algorithms), Second Edition (1981),
  663. X                   Addison-Wesley.  Table 2, page 660.)
  664. X                   Do the following 16 operations:
  665. X                         [A B C D 0  3]
  666. X                         [D A B C 8  9]
  667. X                         [C D A B 4  11]
  668. X                         [B C D A 12 15]
  669. X                         [A B C D 2  3]
  670. X                         [D A B C 10 9]
  671. X                         [C D A B 6  11]
  672. X                         [B C D A 14 15]
  673. X                         [A B C D 1  3]
  674. X                         [D A B C 9  9]
  675. X                         [C D A B 5  11]
  676. X                         [B C D A 13 15]
  677. X                         [A B C D 3  3]
  678. X                         [D A B C 11 9]
  679. X                         [C D A B 7  11]
  680. X                         [B C D A 15 15]
  681. X         Then perform the following additions:
  682. X                         A = A + AA
  683. X                         B = B + BB
  684. XRivest                                                          [Page 5]
  685. X
  686. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  687. X                         C = C + CC
  688. X                         D = D + DD
  689. X         (That is, each of the four registers is incremented by
  690. X         the value it had before this block was started.)
  691. X         end /* of loop on i */
  692. X      Step 5. Output
  693. X         The message digest produced as output is A,B,C,D.  That is, we
  694. X         begin with the low-order byte of A, and end with the high-order
  695. X         byte of D.
  696. X         This completes the description of MD4.  A reference
  697. X         implementation in C is given in the Appendix.
  698. X4.  Extensions
  699. X   If more than 128 bits of output are required, then the following
  700. X   procedure is recommended to obtain a 256-bit output.  (There is no
  701. X   provision made for obtaining more than 256 bits.)
  702. X   Two copies of MD4 are run in parallel over the input.  The first copy
  703. X   is standard as described above.  The second copy is modified as
  704. X   follows.
  705. X   The initial state of the second copy is:
  706. X                    word A:    00 11 22 33
  707. X                    word B:    44 55 66 77
  708. X                    word C:    88 99 aa bb
  709. X                    word D:    cc dd ee ff
  710. X   The magic constants in rounds 2 and 3 for the second copy of MD4 are
  711. X   changed from sqrt(2) and sqrt(3) to cuberoot(2) and cuberoot(3):
  712. X                                    Octal           Hex
  713. X            Round 2 constant        012050505746    50a28be6
  714. X            Round 3 constant        013423350444    5c4dd124
  715. X   Finally, after every 16-word block is processed (including the last
  716. X   block), the values of the A registers in the two copies are
  717. X   exchanged.
  718. X   The final message digest is obtaining by appending the result of the
  719. X   second copy of MD4 to the end of the result of the first copy of MD4.
  720. XRivest                                                          [Page 6]
  721. X
  722. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  723. X5.  Summary
  724. X   The MD4 message digest algorithm is simple to implement, and provides
  725. X   a "fingerprint" or message digest of a message of arbitrary length.
  726. X   It is conjectured that the difficulty of coming up with two messages
  727. X   having the same message digest is on the order of 2^64 operations,
  728. X   and that the difficulty of coming up with any message having a given
  729. X   message digest is on the order of 2^128 operations.  The MD4
  730. X   algorithm has been carefully scrutinized for weaknesses.  It is,
  731. X   however, a relatively new algorithm and further security analysis is
  732. X   of course justified, as is the case with any new proposal of this
  733. X   sort.  The level of security provided by MD4 should be sufficient for
  734. X   implementing very high security hybrid digital signature schemes
  735. X   based on MD4 and the RSA public-key cryptosystem.
  736. X6.  Acknowledgements
  737. X   I'd like to thank Don Coppersmith, Burt Kaliski, Ralph Merkle, and
  738. X   Noam Nisan for numerous helpful comments and suggestions.
  739. XAPPENDIX - Reference Implementation
  740. XThis appendix contains the following files:
  741. X         md4.h        -- header file for using MD4 implementation
  742. X         md4.c        -- the source code for MD4 routines
  743. X         md4driver.c  -- a sample "user" routine
  744. X         session      -- sample results of running md4driver
  745. X /*
  746. X ** ********************************************************************
  747. X ** md4.h -- Header file for implementation of                        **
  748. X ** MD4 Message Digest Algorithm                                      **
  749. X ** Updated: 2/13/90 by Ronald L. Rivest                              **
  750. X ** (C) 1990 RSA Data Security, Inc.                                  **
  751. X ** ********************************************************************
  752. X */
  753. X /* MDstruct is the data structure for a message digest computation.
  754. X */
  755. X typedef struct {
  756. X   unsigned int buffer[4]; /* Holds 4-word result of MD computation */
  757. X   unsigned char count[8]; /* Number of bits processed so far */
  758. X   unsigned int done;      /* Nonzero means MD computation finished */
  759. X } MDstruct, *MDptr;
  760. X /* MDbegin(MD)
  761. XRivest                                                          [Page 7]
  762. X
  763. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  764. X ** Input: MD -- an MDptr
  765. X ** Initialize the MDstruct prepatory to doing a message digest
  766. X ** computation.
  767. X */
  768. X extern void MDbegin();
  769. X /* MDupdate(MD,X,count)
  770. X ** Input: MD -- an MDptr
  771. X **        X -- a pointer to an array of unsigned characters.
  772. X **        count -- the number of bits of X to use (an unsigned int).
  773. X ** Updates MD using the first "count" bits of X.
  774. X ** The array pointed to by X is not modified.
  775. X ** If count is not a multiple of 8, MDupdate uses high bits of
  776. X ** last byte.
  777. X ** This is the basic input routine for a user.
  778. X ** The routine terminates the MD computation when count < 512, so
  779. X ** every MD computation should end with one call to MDupdate with a
  780. X ** count less than 512.  Zero is OK for a count.
  781. X */
  782. X extern void MDupdate();
  783. X /* MDprint(MD)
  784. X ** Input: MD -- an MDptr
  785. X ** Prints message digest buffer MD as 32 hexadecimal digits.
  786. X ** Order is from low-order byte of buffer[0] to high-order byte
  787. X ** of buffer[3].
  788. X ** Each byte is printed with high-order hexadecimal digit first.
  789. X */
  790. X extern void MDprint();
  791. X /*
  792. X ** End of md4.h
  793. X ****************************(cut)***********************************/
  794. X /*
  795. X ** ********************************************************************
  796. X ** md4.c -- Implementation of MD4 Message Digest Algorithm           **
  797. X ** Updated: 2/16/90 by Ronald L. Rivest                              **
  798. X ** (C) 1990 RSA Data Security, Inc.                                  **
  799. X ** ********************************************************************
  800. X */
  801. X /*
  802. X ** To use MD4:
  803. X **   -- Include md4.h in your program
  804. X **   -- Declare an MDstruct MD to hold the state of the digest
  805. X **          computation.
  806. X **   -- Initialize MD using MDbegin(&MD)
  807. XRivest                                                          [Page 8]
  808. X
  809. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  810. X **   -- For each full block (64 bytes) X you wish to process, call
  811. X **          MDupdate(&MD,X,512)
  812. X **      (512 is the number of bits in a full block.)
  813. X **   -- For the last block (less than 64 bytes) you wish to process,
  814. X **          MDupdate(&MD,X,n)
  815. X **      where n is the number of bits in the partial block. A partial
  816. X **      block terminates the computation, so every MD computation
  817. X **      should terminate by processing a partial block, even if it
  818. X **      has n = 0.
  819. X **   -- The message digest is available in MD.buffer[0] ...
  820. X **      MD.buffer[3].  (Least-significant byte of each word
  821. X **      should be output first.)
  822. X **   -- You can print out the digest using MDprint(&MD)
  823. X */
  824. X /* Implementation notes:
  825. X ** This implementation assumes that ints are 32-bit quantities.
  826. X ** If the machine stores the least-significant byte of an int in the
  827. X ** least-addressed byte (e.g., VAX and 8086), then LOWBYTEFIRST
  828. X ** should be set to TRUE.  Otherwise (e.g., SUNS), LOWBYTEFIRST
  829. X ** should be set to FALSE.  Note that on machines with LOWBYTEFIRST
  830. X ** FALSE the routine MDupdate modifies has a side-effect on its input
  831. X ** array (the order of bytes in each word are reversed).  If this is
  832. X ** undesired a call to MDreverse(X) can reverse the bytes of X back
  833. X ** into order after each call to MDupdate.
  834. X */
  835. X #define TRUE  1
  836. X #define FALSE 0
  837. X #define LOWBYTEFIRST FALSE
  838. X /* Compile-time includes
  839. X */
  840. X #include <stdio.h>
  841. X #include "md4.h"
  842. X /* Compile-time declarations of MD4 "magic constants".
  843. X */
  844. X #define I0  0x67452301       /* Initial values for MD buffer */
  845. X #define I1  0xefcdab89
  846. X #define I2  0x98badcfe
  847. X #define I3  0x10325476
  848. X #define C2  013240474631     /* round 2 constant = sqrt(2) in octal */
  849. X #define C3  015666365641     /* round 3 constant = sqrt(3) in octal */
  850. X /* C2 and C3 are from Knuth, The Art of Programming, Volume 2
  851. X ** (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
  852. X ** Table 2, page 660.
  853. X */
  854. XRivest                                                          [Page 9]
  855. X
  856. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  857. X #define fs1  3               /* round 1 shift amounts */
  858. X #define fs2  7
  859. X #define fs3 11
  860. X #define fs4 19
  861. X #define gs1  3               /* round 2 shift amounts */
  862. X #define gs2  5
  863. X #define gs3  9
  864. X #define gs4 13
  865. X #define hs1  3               /* round 3 shift amounts */
  866. X #define hs2  9
  867. X #define hs3 11
  868. X #define hs4 15
  869. X /* Compile-time macro declarations for MD4.
  870. X ** Note: The "rot" operator uses the variable "tmp".
  871. X ** It assumes tmp is declared as unsigned int, so that the >>
  872. X ** operator will shift in zeros rather than extending the sign bit.
  873. X */
  874. X #define f(X,Y,Z)             ((X&Y) | ((~X)&Z))
  875. X #define g(X,Y,Z)             ((X&Y) | (X&Z) | (Y&Z))
  876. X #define h(X,Y,Z)             (X^Y^Z)
  877. X #define rot(X,S)             (tmp=X,(tmp<<S) | (tmp>>(32-S)))
  878. X #define ff(A,B,C,D,i,s)      A = rot((A + f(B,C,D) + X[i]),s)
  879. X #define gg(A,B,C,D,i,s)      A = rot((A + g(B,C,D) + X[i] + C2),s)
  880. X #define hh(A,B,C,D,i,s)      A = rot((A + h(B,C,D) + X[i] + C3),s)
  881. X /* MDprint(MDp)
  882. X ** Print message digest buffer MDp as 32 hexadecimal digits.
  883. X ** Order is from low-order byte of buffer[0] to high-order byte of
  884. X ** buffer[3].
  885. X ** Each byte is printed with high-order hexadecimal digit first.
  886. X ** This is a user-callable routine.
  887. X */
  888. X void
  889. X MDprint(MDp)
  890. X MDptr MDp;
  891. X { int i,j;
  892. X   for (i=0;i<4;i++)
  893. X     for (j=0;j<32;j=j+8)
  894. X       printf("%02x",(MDp->buffer[i]>>j) & 0xFF);
  895. X }
  896. X /* MDbegin(MDp)
  897. X ** Initialize message digest buffer MDp.
  898. X ** This is a user-callable routine.
  899. X */
  900. X void
  901. X MDbegin(MDp)
  902. XRivest                                                         [Page 10]
  903. X
  904. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  905. X MDptr MDp;
  906. X { int i;
  907. X   MDp->buffer[0] = I0;
  908. X   MDp->buffer[1] = I1;
  909. X   MDp->buffer[2] = I2;
  910. X   MDp->buffer[3] = I3;
  911. X   for (i=0;i<8;i++) MDp->count[i] = 0;
  912. X   MDp->done = 0;
  913. X }
  914. X /* MDreverse(X)
  915. X ** Reverse the byte-ordering of every int in X.
  916. X ** Assumes X is an array of 16 ints.
  917. X ** The macro revx reverses the byte-ordering of the next word of X.
  918. X */
  919. X #define revx { t = (*X << 16) | (*X >> 16); \
  920. X      *X++ = ((t & 0xFF00FF00) >> 8) | ((t & 0x00FF00FF) << 8); }
  921. X MDreverse(X)
  922. X unsigned int *X;
  923. X { register unsigned int t;
  924. X   revx; revx; revx; revx; revx; revx; revx; revx;
  925. X   revx; revx; revx; revx; revx; revx; revx; revx;
  926. X }
  927. X /* MDblock(MDp,X)
  928. X ** Update message digest buffer MDp->buffer using 16-word data block X.
  929. X ** Assumes all 16 words of X are full of data.
  930. X ** Does not update MDp->count.
  931. X ** This routine is not user-callable.
  932. X */
  933. X static void
  934. X MDblock(MDp,X)
  935. X MDptr MDp;
  936. X unsigned int *X;
  937. X {
  938. X   register unsigned int tmp, A, B, C, D;
  939. X #if LOWBYTEFIRST == FALSE
  940. X   MDreverse(X);
  941. X #endif
  942. X   A = MDp->buffer[0];
  943. X   B = MDp->buffer[1];
  944. X   C = MDp->buffer[2];
  945. X   D = MDp->buffer[3];
  946. X   /* Update the message digest buffer */
  947. X   ff(A , B , C , D ,  0 , fs1); /* Round 1 */
  948. X   ff(D , A , B , C ,  1 , fs2);
  949. X   ff(C , D , A , B ,  2 , fs3);
  950. X   ff(B , C , D , A ,  3 , fs4);
  951. XRivest                                                         [Page 11]
  952. X
  953. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  954. X   ff(A , B , C , D ,  4 , fs1);
  955. X   ff(D , A , B , C ,  5 , fs2);
  956. X   ff(C , D , A , B ,  6 , fs3);
  957. X   ff(B , C , D , A ,  7 , fs4);
  958. X   ff(A , B , C , D ,  8 , fs1);
  959. X   ff(D , A , B , C ,  9 , fs2);
  960. X   ff(C , D , A , B , 10 , fs3);
  961. X   ff(B , C , D , A , 11 , fs4);
  962. X   ff(A , B , C , D , 12 , fs1);
  963. X   ff(D , A , B , C , 13 , fs2);
  964. X   ff(C , D , A , B , 14 , fs3);
  965. X   ff(B , C , D , A , 15 , fs4);
  966. X   gg(A , B , C , D ,  0 , gs1); /* Round 2 */
  967. X   gg(D , A , B , C ,  4 , gs2);
  968. X   gg(C , D , A , B ,  8 , gs3);
  969. X   gg(B , C , D , A , 12 , gs4);
  970. X   gg(A , B , C , D ,  1 , gs1);
  971. X   gg(D , A , B , C ,  5 , gs2);
  972. X   gg(C , D , A , B ,  9 , gs3);
  973. X   gg(B , C , D , A , 13 , gs4);
  974. X   gg(A , B , C , D ,  2 , gs1);
  975. X   gg(D , A , B , C ,  6 , gs2);
  976. X   gg(C , D , A , B , 10 , gs3);
  977. X   gg(B , C , D , A , 14 , gs4);
  978. X   gg(A , B , C , D ,  3 , gs1);
  979. X   gg(D , A , B , C ,  7 , gs2);
  980. X   gg(C , D , A , B , 11 , gs3);
  981. X   gg(B , C , D , A , 15 , gs4);
  982. X   hh(A , B , C , D ,  0 , hs1); /* Round 3 */
  983. X   hh(D , A , B , C ,  8 , hs2);
  984. X   hh(C , D , A , B ,  4 , hs3);
  985. X   hh(B , C , D , A , 12 , hs4);
  986. X   hh(A , B , C , D ,  2 , hs1);
  987. X   hh(D , A , B , C , 10 , hs2);
  988. X   hh(C , D , A , B ,  6 , hs3);
  989. X   hh(B , C , D , A , 14 , hs4);
  990. X   hh(A , B , C , D ,  1 , hs1);
  991. X   hh(D , A , B , C ,  9 , hs2);
  992. X   hh(C , D , A , B ,  5 , hs3);
  993. X   hh(B , C , D , A , 13 , hs4);
  994. X   hh(A , B , C , D ,  3 , hs1);
  995. X   hh(D , A , B , C , 11 , hs2);
  996. X   hh(C , D , A , B ,  7 , hs3);
  997. X   hh(B , C , D , A , 15 , hs4);
  998. X   MDp->buffer[0] += A;
  999. X   MDp->buffer[1] += B;
  1000. X   MDp->buffer[2] += C;
  1001. X   MDp->buffer[3] += D;
  1002. XRivest                                                         [Page 12]
  1003. X
  1004. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  1005. X }
  1006. X /* MDupdate(MDp,X,count)
  1007. X ** Input: MDp -- an MDptr
  1008. X **        X -- a pointer to an array of unsigned characters.
  1009. X **        count -- the number of bits of X to use.
  1010. X **          (if not a multiple of 8, uses high bits of last byte.)
  1011. X ** Update MDp using the number of bits of X given by count.
  1012. X ** This is the basic input routine for an MD4 user.
  1013. X ** The routine completes the MD computation when count < 512, so
  1014. X ** every MD computation should end with one call to MDupdate with a
  1015. X ** count less than 512.  A call with count 0 will be ignored if the
  1016. X ** MD has already been terminated (done != 0), so an extra call with
  1017. X ** count 0 can be given as a "courtesy close" to force termination
  1018. X ** if desired.
  1019. X */
  1020. X void
  1021. X MDupdate(MDp,X,count)
  1022. X MDptr MDp;
  1023. X unsigned char *X;
  1024. X unsigned int count;
  1025. X { unsigned int i, tmp, bit, byte, mask;
  1026. X   unsigned char XX[64];
  1027. X   unsigned char *p;
  1028. X   /* return with no error if this is a courtesy close with count
  1029. X   ** zero and MDp->done is true.
  1030. X   */
  1031. X   if (count == 0 && MDp->done) return;
  1032. X   /* check to see if MD is already done and report error */
  1033. X   if (MDp->done)
  1034. X          { printf("\nError: MDupdate MD already done."); return; }
  1035. X   /* Add count to MDp->count */
  1036. X   tmp = count;
  1037. X   p = MDp->count;
  1038. X   while (tmp)
  1039. X     { tmp += *p;
  1040. X       *p++ = tmp;
  1041. X       tmp = tmp >> 8;
  1042. X     }
  1043. X   /* Process data */
  1044. X   if (count == 512)
  1045. X     { /* Full block of data to handle */
  1046. X       MDblock(MDp,(unsigned int *)X);
  1047. X     }
  1048. X   else if (count > 512) /* Check for count too large */
  1049. X     { printf("\nError: MDupdate called with illegal count value %d."
  1050. X              ,count);
  1051. X       return;
  1052. XRivest                                                         [Page 13]
  1053. X
  1054. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  1055. X     }
  1056. X   else /* partial block -- must be last block so finish up */
  1057. X     { /* Find out how many bytes and residual bits there are */
  1058. X       byte = count >> 3;
  1059. X       bit =  count & 7;
  1060. X       /* Copy X into XX since we need to modify it */
  1061. X       for (i=0;i<=byte;i++)   XX[i] = X[i];
  1062. X       for (i=byte+1;i<64;i++) XX[i] = 0;
  1063. X       /* Add padding '1' bit and low-order zeros in last byte */
  1064. X       mask = 1 << (7 - bit);
  1065. X       XX[byte] = (XX[byte] | mask) & ~( mask - 1);
  1066. X       /* If room for bit count, finish up with this block */
  1067. X       if (byte <= 55)
  1068. X         { for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
  1069. X           MDblock(MDp,(unsigned int *)XX);
  1070. X         }
  1071. X       else /* need to do two blocks to finish up */
  1072. X         { MDblock(MDp,(unsigned int *)XX);
  1073. X           for (i=0;i<56;i++) XX[i] = 0;
  1074. X           for (i=0;i<8;i++)  XX[56+i] = MDp->count[i];
  1075. X           MDblock(MDp,(unsigned int *)XX);
  1076. X         }
  1077. X       /* Set flag saying we're done with MD computation */
  1078. X       MDp->done = 1;
  1079. X     }
  1080. X }
  1081. X /*
  1082. X ** End of md4.c
  1083. X ****************************(cut)***********************************/
  1084. X /*
  1085. X ** ********************************************************************
  1086. X ** md4driver.c -- sample routines to test                            **
  1087. X ** MD4 message digest algorithm.                                     **
  1088. X ** Updated: 2/16/90 by Ronald L. Rivest                              **
  1089. X ** (C) 1990 RSA Data Security, Inc.                                  **
  1090. X ** ********************************************************************
  1091. X */
  1092. X #include <stdio.h>
  1093. X #include "md4.h"
  1094. X /* MDtimetrial()
  1095. X ** A time trial routine, to measure the speed of MD4.
  1096. X ** Measures speed for 1M blocks = 64M bytes.
  1097. X */
  1098. X MDtimetrial()
  1099. XRivest                                                         [Page 14]
  1100. X
  1101. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  1102. X { unsigned int X[16];
  1103. X   MDstruct MD;
  1104. X   int i;
  1105. X   double t;
  1106. X   for (i=0;i<16;i++) X[i] = 0x01234567 + i;
  1107. X   printf
  1108. X   ("MD4 time trial. Processing 1 million 64-character blocks...\n");
  1109. X   clock();
  1110. X   MDbegin(&MD);
  1111. X   for (i=0;i<1000000;i++) MDupdate(&MD,X,512);
  1112. X   MDupdate(&MD,X,0);
  1113. X   t = (double) clock(); /* in microseconds */
  1114. X   MDprint(&MD); printf(" is digest of 64M byte test input.\n");
  1115. X   printf("Seconds to process test input:   %g\n,t/1e6);
  1116. X   printf("Characters processed per second: %ld.\n,(int)(64e12/t));
  1117. X }
  1118. X /* MDstring(s)
  1119. X ** Computes the message digest for string s.
  1120. X ** Prints out message digest, a space, the string (in quotes) and a
  1121. X ** carriage return.
  1122. X */
  1123. X MDstring(s)
  1124. X unsigned char *s;
  1125. X { unsigned int i, len = strlen(s);
  1126. X   MDstruct MD;
  1127. X   MDbegin(&MD);
  1128. X   for (i=0;i+64<=len;i=i+64) MDupdate(&MD,s+i,512);
  1129. X   MDupdate(&MD,s+i,(len-i)*8);
  1130. X   MDprint(&MD);
  1131. X   printf(" \"%s\"\n",s);
  1132. X }
  1133. X /* MDfile(filename)
  1134. X ** Computes the message digest for a specified file.
  1135. X ** Prints out message digest, a space, the file name, and a
  1136. X ** carriage return.
  1137. X */
  1138. X MDfile(filename)
  1139. X char *filename;
  1140. X { FILE *f = fopen(filename,"rb");
  1141. X   unsigned char X[64];
  1142. X   MDstruct MD;
  1143. X   int b;
  1144. X   if (f == NULL)
  1145. X      { printf("%s can't be opened.\n",filename); return; }
  1146. X   MDbegin(&MD);
  1147. X   while ((b=fread(X,1,64,f))!=0) MDupdate(&MD,X,b*8);
  1148. XRivest                                                         [Page 15]
  1149. X
  1150. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  1151. X   MDupdate(&MD,X,0);
  1152. X   MDprint(&MD);
  1153. X   printf(" %s\n",filename);
  1154. X   fclose(f);
  1155. X }
  1156. X /* MDfilter()
  1157. X ** Writes the message digest of the data from stdin onto stdout,
  1158. X ** followed by a carriage return.
  1159. X */
  1160. X MDfilter()
  1161. X { unsigned char X[64];
  1162. X   MDstruct MD;
  1163. X   int b;
  1164. X   MDbegin(&MD);
  1165. X   while ((b=fread(X,1,64,stdin))!=0) MDupdate(&MD,X,b*8);
  1166. X   MDupdate(&MD,X,0);
  1167. X   MDprint(&MD);
  1168. X   printf("\n");
  1169. X }
  1170. X /* MDtestsuite()
  1171. X ** Run a standard suite of test data.
  1172. X */
  1173. X MDtestsuite()
  1174. X {
  1175. X   printf("MD4 test suite results:\n");
  1176. X   MDstring("");
  1177. X   MDstring("a");
  1178. X   MDstring("abc");
  1179. X   MDstring("message digest");
  1180. X   MDstring("abcdefghijklmnopqrstuvwxyz");
  1181. X   MDstring
  1182. X   ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  1183. X   MDfile("foo"); /* Contents of file foo are "abc" */
  1184. X }
  1185. X main(argc,argv)
  1186. X int argc;
  1187. X char *argv[];
  1188. X { int i;
  1189. X   /* For each command line argument in turn:
  1190. X   ** filename          -- prints message digest and name of file
  1191. X   ** -sstring          -- prints message digest and contents of string
  1192. X   ** -t                -- prints time trial statistics for 64M bytes
  1193. X   ** -x                -- execute a standard suite of test data
  1194. X   ** (no args)         -- writes messages digest of stdin onto stdout
  1195. X   */
  1196. XRivest                                                         [Page 16]
  1197. X
  1198. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  1199. X   if (argc==1) MDfilter();
  1200. X   else
  1201. X     for (i=1;i<argc;i++)
  1202. X       if (argv[i][0]=='-' && argv[i][1]=='s') MDstring(argv[i]+2);
  1203. X       else if (strcmp(argv[i],"-t")==0)       MDtimetrial();
  1204. X       else if (strcmp(argv[i],"-x")==0)       MDtestsuite();
  1205. X       else                                    MDfile(argv[i]);
  1206. X }
  1207. X /*
  1208. X ** end of md4driver.c
  1209. X ****************************(cut)***********************************/
  1210. X --------------------------------------------------------------------
  1211. X --- Sample session.  Compiling and using MD4 on SUN Sparcstation ---
  1212. X --------------------------------------------------------------------
  1213. X >ls
  1214. X total 66
  1215. X -rw-rw-r--  1 rivest          3 Feb 14 17:40 abcfile
  1216. X -rwxrwxr-x  1 rivest      24576 Feb 17 12:28 md4
  1217. X -rw-rw-r--  1 rivest       9347 Feb 17 00:37 md4.c
  1218. X -rw-rw-r--  1 rivest      25150 Feb 17 12:25 md4.doc
  1219. X -rw-rw-r--  1 rivest       1844 Feb 16 21:21 md4.h
  1220. X -rw-rw-r--  1 rivest       3497 Feb 17 12:27 md4driver.c
  1221. X >
  1222. X >cc -o md4 -O4 md4.c md4driver.c
  1223. X md4.c:
  1224. X md4driver.c:
  1225. X Linking:
  1226. X >
  1227. X >md4 -x
  1228. X MD4 test suite results:
  1229. X 31d6cfe0d16ae931b73c59d7e0c089c0 ""
  1230. X bde52cb31de33e46245e05fbdbd6fb24 "a"
  1231. X a448017aaf21d8525fc10ae87aa6729d "abc"
  1232. X d9130a8164549fe818874806e1c7014b "message digest"
  1233. X d79e1c308aa5bbcdeea8ed63df412da9 "abcdefghijklmnopqrstuvwxyz"
  1234. X 043f8582f241db351ce627e153e7f0e4
  1235. X      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  1236. X a448017aaf21d8525fc10ae87aa6729d abcfile
  1237. X >
  1238. X >md4 -sabc -shi
  1239. X a448017aaf21d8525fc10ae87aa6729d "abc"
  1240. X cfaee2512bd25eb033236f0cd054e308 "hi"
  1241. X >
  1242. X >md4 *
  1243. X a448017aaf21d8525fc10ae87aa6729d abcfile
  1244. XRivest                                                         [Page 17]
  1245. X
  1246. XRFC 1186              MD4 Message Digest Algorithm          October 1990
  1247. X d316f994da0e951cf9502928a1f73300 md4
  1248. X 379adb39eada0dfdbbdfdcd0d9def8c4 md4.c
  1249. X 9a3f73327c65954198b1f45a3aa12665 md4.doc
  1250. X 37fe165ac177b461ff78b86d10e4ff33 md4.h
  1251. X 7dcba2e2dc4d8f1408d08beb17dabb2a md4.o
  1252. X 08790161bfddc6f5788b4353875cb1c3 md4driver.c
  1253. X 1f84a7f690b0545d2d0480d5d3c26eea md4driver.o
  1254. X >
  1255. X >cat abcfile | md4
  1256. X a448017aaf21d8525fc10ae87aa6729d
  1257. X >
  1258. X >md4 -t
  1259. X MD4 time trial. Processing 1 million 64-character blocks...
  1260. X 6325bf77e5891c7c0d8104b64cc6e9ef is digest of 64M byte test input.
  1261. X Seconds to process test input:   44.0982
  1262. X Characters processed per second: 1451305.
  1263. X >
  1264. X >
  1265. X ------------------------ end of sample session --------------------
  1266. X      Note:  A version of this document including the C source code is
  1267. X      available for FTP from THEORY.LSC.MIT.EDU in the file "md4.doc".
  1268. XSecurity Considerations
  1269. X   The level of security discussed in this memo by MD4 is considered to
  1270. X   be sufficient for implementing very high security hybrid digital
  1271. X   signature schemes based on MD4 and the RSA public-key cryptosystem.
  1272. XAuthor's Address
  1273. X   Ronald L. Rivest
  1274. X   Massachusetts Institute of Technology
  1275. X   Laboratory for Computer Science
  1276. X   NE43-324
  1277. X   545 Technology Square
  1278. X   Cambridge, MA 02139-1986
  1279. X   Phone: (617) 253-5880
  1280. X   EMail: rivest@theory.lcs.mit.edu
  1281. XRivest                                                         [Page 18]
  1282. X
  1283. END_OF_FILE
  1284.   if test 34579 -ne `wc -c <'rfc1186'`; then
  1285.     echo shar: \"'rfc1186'\" unpacked with wrong size!
  1286.   fi
  1287.   # end of 'rfc1186'
  1288. fi
  1289. echo shar: End of archive 1 \(of 2\).
  1290. cp /dev/null ark1isdone
  1291. MISSING=""
  1292. for I in 1 2 ; do
  1293.     if test ! -f ark${I}isdone ; then
  1294.     MISSING="${MISSING} ${I}"
  1295.     fi
  1296. done
  1297. if test "${MISSING}" = "" ; then
  1298.     echo You have unpacked both archives.
  1299.     rm -f ark[1-9]isdone
  1300. else
  1301.     echo You still must unpack the following archives:
  1302.     echo "        " ${MISSING}
  1303. fi
  1304. exit 0
  1305. exit 0 # Just in case...
  1306. -- 
  1307. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1308. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1309. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1310. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1311. exit 0 # Just in case...
  1312. -- 
  1313. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1314. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1315. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1316. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1317.