home *** CD-ROM | disk | FTP | other *** search
/ PDA Software Library / pdasoftwarelib.iso / PSION / MISC / PSI-KEY / MD4.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-17  |  9.9 KB  |  359 lines

  1. /* Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.
  2.  *
  3.  * License to copy and use this software is granted provided that it
  4.  * is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  5.  * Algorithm" in all material mentioning or referencing this software
  6.  * or this function.
  7.  *
  8.  * License is also granted to make and use derivative works provided
  9.  * that such works are identified as "derived from the RSA Data
  10.  * Security, Inc. MD4 Message-Digest Algorithm" in all material
  11.  * mentioning or referencing the derived work.
  12.  *
  13.  * RSA Data Security, Inc. makes no representations concerning either
  14.  * the merchantability of this software or the suitability of this
  15.  * software for any particular purpose. It is provided "as is"
  16.  * without express or implied warranty of any kind.
  17.  *
  18.  * These notices must be retained in any copies of any part of this
  19.  * documentation and/or software.
  20.    */
  21.   
  22. /* 
  23.  * md4.c -- Implementation of MD4 Message Digest Algorithm
  24.  * Updated: 2/16/90 by Ronald L. Rivest
  25.  * (C) 1990 RSA Data Security, Inc.
  26.  *
  27.  * Portability nits fixed and reformatted - 2/12/91 Phil Karn
  28.  *
  29.  * PSION version: 7/7/95 Richard Letts <R.J.Letts@salford.ac.uk>
  30.  *     plib used instead of clib 
  31.  *     some functions removed
  32.  *            
  33.  */
  34.  
  35.  
  36.  
  37. /* 
  38.  * To use MD4:
  39.  *   -- Include md4.h in your program
  40.  *   -- Declare an MDstruct MD to hold the state of the digest computation.
  41.  *   -- Initialize MD using MDbegin(&MD)
  42.  *   -- For each full block (64 bytes) X you wish to process, call
  43.  *          MDupdate(&MD,X,512)
  44.  *      (512 is the number of bits in a full block.)
  45.  *   -- For the last block (less than 64 bytes) you wish to process,
  46.  *          MDupdate(&MD,X,n)
  47.  *      where n is the number of bits in the partial block. A partial
  48.  *      block terminates the computation, so every MD computation should
  49.  *      terminate by processing a partial block, even if it has n = 0.
  50.  *   -- The message digest is available in MD.buffer[0] ... MD.buffer[3].
  51.  *      (Least-significant byte of each word should be output first.)
  52.  *   -- You can print out the digest using MDprint(&MD)
  53.  */
  54.  
  55. /* Implementation notes:
  56.  * This implementation assumes that longs are 32-bit quantities.
  57.  * If the machine stores the least-significant byte of an long in the
  58.  * least-addressed byte (eg., VAX and 8086), then LOWBYTEFIRST should be
  59.  * set to TRUE.  Otherwise (eg., SUNS), LOWBYTEFIRST should be set to
  60.  * FALSE.  Note that on machines with LOWBYTEFIRST FALSE the routine
  61.  * MDupdate modifies has a side-effect on its input array (the order of bytes
  62.  * in each word are reversed).  If this is undesired a call to MDreverse(X) can
  63.  * reverse the bytes of X back into order after each call to MDupdate.
  64.  */
  65. #define TRUE  1
  66. #define FALSE 0
  67.  
  68.  
  69. #if (defined(__MSDOS__) || defined(MPU8086) || defined(MPU8080) \
  70.  || defined(vax) || defined (MIPSEL))
  71. #define LOWBYTEFIRST TRUE    /* Low order bytes are first in memory */
  72. #else            /* Almost all other machines are big-endian */
  73. #define    LOWBYTEFIRST FALSE
  74. #endif
  75.  
  76.  
  77. /* Compile-time includes */
  78. #include <plib.h>
  79. #include "md4.h"
  80.  
  81. void MDblock(MDptr MDp,unsigned long *X);
  82. void MDreverse(unsigned long *X);
  83. void MDRound1(MDptr MDp,unsigned long *X);
  84. void MDRound2(MDptr MDp,unsigned long *X);
  85. void MDRound3(MDptr MDp,unsigned long *X);
  86. void MDreverse(unsigned long *X);
  87.  
  88. /* Compile-time declarations of MD4 ``magic constants'' */
  89. #define I0  0x67452301       /* Initial values for MD buffer */
  90. #define I1  0xefcdab89
  91. #define I2  0x98badcfe
  92. #define I3  0x10325476
  93. #define C2  013240474631     /* round 2 constant = sqrt(2) in octal */
  94. #define C3  015666365641     /* round 3 constant = sqrt(3) in octal */
  95. /* C2 and C3 are from Knuth, The Art of Programming, Volume 2
  96.  * (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
  97.  * Table 2, page 660.
  98.  */
  99. #define fs1  3               /* round 1 shift amounts */
  100. #define fs2  7   
  101. #define fs3 11  
  102. #define fs4 19  
  103. #define gs1  3               /* round 2 shift amounts */
  104. #define gs2  5   
  105. #define gs3  9   
  106. #define gs4 13  
  107. #define hs1  3               /* round 3 shift amounts */
  108. #define hs2  9 
  109. #define hs3 11 
  110. #define hs4 15
  111.  
  112.  
  113. /* Compile-time macro declarations for MD4.
  114.  * Note: The ``rot'' operator uses the variable ``tmp''.
  115.  * It assumes tmp is declared as unsigned long, so that the >>
  116.  * operator will shift in zeros rather than extending the sign bit.
  117.  */
  118. #define    f(X,Y,Z)             ((X&Y) | ((~X)&Z))
  119. #define    g(X,Y,Z)             ((X&Y) | (X&Z) | (Y&Z))
  120. #define h(X,Y,Z)             (X^Y^Z)
  121. #define rot(X,S)             (tmp=X,(tmp<<(int)S) | (tmp>>(int)(32-S)))
  122. #define ff(A,B,C,D,i,s)      A = rot((A + f(B,C,D) + X[i]),s)
  123. #define gg(A,B,C,D,i,s)      A = rot((A + g(B,C,D) + X[i] + C2),s)
  124. #define hh(A,B,C,D,i,s)      A = rot((A + h(B,C,D) + X[i] + C3),s)
  125.  
  126.  
  127. /* MDbegin(MDp)
  128.  * Initialize message digest buffer MDp. 
  129.  * This is a user-callable routine.
  130.  */
  131. void 
  132. MDbegin(MDp)
  133. MDptr MDp;
  134. {
  135.     int i;
  136.  
  137.     MDp->buffer[0] = I0;  
  138.     MDp->buffer[1] = I1;  
  139.     MDp->buffer[2] = I2;  
  140.     MDp->buffer[3] = I3; 
  141.     for(i=0;i<8;i++)
  142.         MDp->count[i] = 0;
  143.     MDp->done = 0;
  144. }
  145.  
  146. /* MDreverse(X)
  147.  * Reverse the byte-ordering of every long in X.
  148.  * Assumes X is an array of 16 longs.
  149.  * The macro revx reverses the byte-ordering of the next word of X.
  150.  */
  151. #define revx { t = (*X << 16) | (*X >> 16); \
  152.            *X++ = ((t & 0xFF00FF00) >> 8) | ((t & 0x00FF00FF) << 8); }
  153. void
  154. MDreverse(X)
  155. unsigned long *X;
  156. {
  157.     register unsigned long t;
  158.  
  159.     revx;
  160.     revx;
  161.     revx;
  162.     revx;
  163.     revx;
  164.     revx;
  165.     revx;
  166.     revx;
  167.     revx;
  168.     revx;
  169.     revx;
  170.     revx;
  171.     revx;
  172.     revx;
  173.     revx;
  174.     revx;
  175. }
  176.  
  177.  
  178. /* MDupdate(MDp,X,count)
  179.  * Input: MDp -- an MDptr
  180.  *        X -- a pointer to an array of unsigned characters.
  181.  *        count -- the number of bits of X to use.
  182.  *                 (if not a multiple of 8, uses high bits of last byte.)
  183.  * Update MDp using the number of bits of X given by count.
  184.  * This is the basic input routine for an MD4 user.
  185.  * The routine completes the MD computation when count < 512, so
  186.  * every MD computation should end with one call to MDupdate with a
  187.  * count less than 512.  A call with count 0 will be ignored if the
  188.  * MD has already been terminated (done != 0), so an extra call with count
  189.  * 0 can be given as a ``courtesy close'' to force termination if desired.
  190.  */
  191. void 
  192. MDupdate(MDp,X,count)
  193. MDptr MDp;
  194. unsigned char *X;
  195. unsigned int count;
  196. {
  197.     int i,bit,byte,mask;
  198.     unsigned long tmp;
  199.     unsigned char XX[64];
  200.     unsigned char *p;
  201.  
  202.     /* return with no error if this is a courtesy close with count
  203.      * zero and MDp->done is true.
  204.      */
  205.     if(count == 0 && MDp->done)
  206.         return;
  207.     /* check to see if MD is already done and report error */
  208.     if(MDp->done){
  209.         p_printf("\nError: MDupdate MD already done.");
  210.         return;
  211.     }
  212.     /* Add count to MDp->count */
  213.     tmp = count;
  214.     p = MDp->count;
  215.     while(tmp){
  216.         tmp += *p;
  217.         *p++ = tmp;
  218.         tmp = tmp >> 8;
  219.     }
  220.     /* Process data */
  221.     if(count == 512){
  222.         /* Full block of data to handle */
  223.         MDblock(MDp,(unsigned long *)X);
  224.     } else if(count > 512){
  225.         /* Check for count too large */
  226.         p_printf("\nError: MDupdate called with illegal count value %ld.",count);
  227.         return;
  228.     } else {
  229.         /* partial block -- must be last block so finish up
  230.          * Find out how many bytes and residual bits there are
  231.          */
  232.         byte = count >> 3;
  233.         bit =  count & 7;
  234.         /* Copy X into XX since we need to modify it */
  235.         for(i=0;i<=byte;i++)
  236.             XX[i] = X[i];
  237.         for(i=byte+1;i<64;i++)
  238.             XX[i] = 0;
  239.         /* Add padding '1' bit and low-order zeros in last byte */
  240.         mask = 1 << (7 - bit);
  241.         XX[byte] = (XX[byte] | mask) & ~( mask - 1);
  242.         /* If room for bit count, finish up with this block */
  243.         if(byte <= 55){
  244.             for(i=0;i<8;i++)
  245.                 XX[56+i] = MDp->count[i];
  246.             MDblock(MDp,(unsigned long *)XX);
  247.         } else {
  248.             /* need to do two blocks to finish up */
  249.             MDblock(MDp,(unsigned long *)XX);
  250.             for(i=0;i<56;i++)
  251.                 XX[i] = 0;
  252.             for(i=0;i<8;i++)
  253.                 XX[56+i] = MDp->count[i];
  254.             MDblock(MDp,(unsigned long *)XX);
  255.         }
  256.     /* Set flag saying we're done with MD computation */
  257.     MDp->done = 1;
  258.     }
  259. }
  260.  
  261.  
  262.  
  263. /* MDblock(MDp,X)
  264.  * Update message digest buffer MDp->buffer using 16-word data block X.
  265.  * Assumes all 16 words of X are full of data.
  266.  * Does not update MDp->count.
  267.  * This routine is not user-callable. 
  268.  */
  269. unsigned long tmp, A, B, C, D;
  270.  
  271. void MDRound1(MDptr MDp,unsigned long *X)
  272. {
  273.     ff(A,B,C,D,0,fs1); /* Round 1 */
  274.     ff(D,A,B,C,1,fs2); 
  275.     ff(C,D,A,B,2,fs3); 
  276.     ff(B,C,D,A,3,fs4); 
  277.     ff(A,B,C,D,4,fs1); 
  278.     ff(D,A,B,C,5,fs2); 
  279.     ff(C,D,A,B,6,fs3); 
  280.     ff(B,C,D,A,7,fs4); 
  281.     ff(A,B,C,D,8,fs1); 
  282.     ff(D,A,B,C,9,fs2); 
  283.     ff(C,D,A,B,10,fs3); 
  284.     ff(B,C,D,A,11,fs4); 
  285.     ff(A,B,C,D,12,fs1); 
  286.     ff(D,A,B,C,13,fs2); 
  287.     ff(C,D,A,B,14,fs3); 
  288.     ff(B,C,D,A,15,fs4); 
  289.     return;
  290. }
  291.  
  292. void MDRound2(MDp,X)
  293. MDptr MDp;
  294. unsigned long *X;
  295. {
  296.     gg(A,B,C,D,0,gs1); /* Round 2 */
  297.     gg(D,A,B,C,4,gs2); 
  298.     gg(C,D,A,B,8,gs3); 
  299.     gg(B,C,D,A,12,gs4); 
  300.     gg(A,B,C,D,1,gs1); 
  301.     gg(D,A,B,C,5,gs2); 
  302.     gg(C,D,A,B,9,gs3); 
  303.     gg(B,C,D,A,13,gs4); 
  304.     gg(A,B,C,D,2,gs1); 
  305.     gg(D,A,B,C,6,gs2); 
  306.     gg(C,D,A,B,10,gs3); 
  307.     gg(B,C,D,A,14,gs4); 
  308.     gg(A,B,C,D,3,gs1); 
  309.     gg(D,A,B,C,7,gs2); 
  310.     gg(C,D,A,B,11,gs3); 
  311.     gg(B,C,D,A,15,gs4);  
  312. }
  313.  
  314. void MDRound3(MDp,X)
  315. MDptr MDp;
  316. unsigned long *X;
  317. {
  318.     hh(A,B,C,D,0,hs1); /* Round 3 */
  319.     hh(D,A,B,C,8,hs2); 
  320.     hh(C,D,A,B,4,hs3); 
  321.     hh(B,C,D,A,12,hs4); 
  322.     hh(A,B,C,D,2,hs1); 
  323.     hh(D,A,B,C,10,hs2); 
  324.     hh(C,D,A,B,6,hs3); 
  325.     hh(B,C,D,A,14,hs4); 
  326.     hh(A,B,C,D,1,hs1); 
  327.     hh(D,A,B,C,9,hs2); 
  328.     hh(C,D,A,B,5,hs3); 
  329.     hh(B,C,D,A,13,hs4); 
  330.     hh(A,B,C,D,3,hs1); 
  331.     hh(D,A,B,C,11,hs2); 
  332.     hh(C,D,A,B,7,hs3); 
  333.     hh(B,C,D,A,15,hs4);
  334. return;
  335. }
  336.  
  337. void MDblock(MDp,X)
  338. MDptr MDp;
  339. unsigned long *X;
  340.  
  341. #if LOWBYTEFIRST == FALSE
  342.     MDreverse(X);
  343. #endif
  344.     A = MDp->buffer[0];
  345.     B = MDp->buffer[1];
  346.     C = MDp->buffer[2];
  347.     D = MDp->buffer[3];
  348.     /* Update the message digest buffer */
  349.     MDRound1(MDp,X);
  350.     MDRound2(MDp,X);
  351.     MDRound3(MDp,X);
  352.     MDp->buffer[0] += A; 
  353.     MDp->buffer[1] += B;
  354.     MDp->buffer[2] += C;
  355.     MDp->buffer[3] += D; 
  356. }
  357. /* End of md4.c */
  358.