md4.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. ** ********************************************************************
  3. ** md4.c -- Implementation of MD4 Message Digest Algorithm **
  4. ** Updated: 2/16/90 by Ronald L. Rivest **
  5. ** (C) 1990 RSA Data Security, Inc. **
  6. ** ********************************************************************
  7. */
  8. /*
  9. ** To use MD4:
  10. ** -- Include md4.h in your program
  11. ** -- Declare an MDstruct MD to hold the state of the digest
  12. ** computation.
  13. ** -- Initialize MD using MDbegin(&MD)
  14. ** -- For each full block (64 bytes) X you wish to process, call
  15. ** MD4Update(&MD,X,512)
  16. ** (512 is the number of bits in a full block.)
  17. ** -- For the last block (less than 64 bytes) you wish to process,
  18. ** MD4Update(&MD,X,n)
  19. ** where n is the number of bits in the partial block. A partial
  20. ** block terminates the computation, so every MD computation
  21. ** should terminate by processing a partial block, even if it
  22. ** has n = 0.
  23. ** -- The message digest is available in MD.buffer[0] ...
  24. ** MD.buffer[3]. (Least-significant byte of each word
  25. ** should be output first.)
  26. ** -- You can print out the digest using MDprint(&MD)
  27. */
  28. /* Implementation notes:
  29. ** This implementation assumes that ints are 32-bit quantities.
  30. */
  31. #define TRUE 1
  32. #define FALSE 0
  33. /* Compile-time includes
  34. */
  35. #include <stdio.h>
  36. #include "md4.h"
  37. #include "pppd.h"
  38. /* Compile-time declarations of MD4 "magic constants".
  39. */
  40. #define I0 0x67452301 /* Initial values for MD buffer */
  41. #define I1 0xefcdab89
  42. #define I2 0x98badcfe
  43. #define I3 0x10325476
  44. #define C2 013240474631 /* round 2 constant = sqrt(2) in octal */
  45. #define C3 015666365641 /* round 3 constant = sqrt(3) in octal */
  46. /* C2 and C3 are from Knuth, The Art of Programming, Volume 2
  47. ** (Seminumerical Algorithms), Second Edition (1981), Addison-Wesley.
  48. ** Table 2, page 660.
  49. */
  50. #define fs1 3 /* round 1 shift amounts */
  51. #define fs2 7
  52. #define fs3 11
  53. #define fs4 19
  54. #define gs1 3 /* round 2 shift amounts */
  55. #define gs2 5
  56. #define gs3 9
  57. #define gs4 13
  58. #define hs1 3 /* round 3 shift amounts */
  59. #define hs2 9
  60. #define hs3 11
  61. #define hs4 15
  62. /* Compile-time macro declarations for MD4.
  63. ** Note: The "rot" operator uses the variable "tmp".
  64. ** It assumes tmp is declared as unsigned int, so that the >>
  65. ** operator will shift in zeros rather than extending the sign bit.
  66. */
  67. #define f(X,Y,Z) ((X&Y) | ((~X)&Z))
  68. #define g(X,Y,Z) ((X&Y) | (X&Z) | (Y&Z))
  69. #define h(X,Y,Z) (X^Y^Z)
  70. #define rot(X,S) (tmp=X,(tmp<<S) | (tmp>>(32-S)))
  71. #define ff(A,B,C,D,i,s) A = rot((A + f(B,C,D) + X[i]),s)
  72. #define gg(A,B,C,D,i,s) A = rot((A + g(B,C,D) + X[i] + C2),s)
  73. #define hh(A,B,C,D,i,s) A = rot((A + h(B,C,D) + X[i] + C3),s)
  74. /* MD4print(MDp)
  75. ** Print message digest buffer MDp as 32 hexadecimal digits.
  76. ** Order is from low-order byte of buffer[0] to high-order byte of
  77. ** buffer[3].
  78. ** Each byte is printed with high-order hexadecimal digit first.
  79. ** This is a user-callable routine.
  80. */
  81. void
  82. MD4Print(MDp)
  83. MD4_CTX *MDp;
  84. {
  85. int i,j;
  86. for (i=0;i<4;i++)
  87. for (j=0;j<32;j=j+8)
  88. printf("%02x",(MDp->buffer[i]>>j) & 0xFF);
  89. }
  90. /* MD4Init(MDp)
  91. ** Initialize message digest buffer MDp.
  92. ** This is a user-callable routine.
  93. */
  94. void
  95. MD4Init(MDp)
  96. MD4_CTX *MDp;
  97. {
  98. int i;
  99. MDp->buffer[0] = I0;
  100. MDp->buffer[1] = I1;
  101. MDp->buffer[2] = I2;
  102. MDp->buffer[3] = I3;
  103. for (i=0;i<8;i++) MDp->count[i] = 0;
  104. MDp->done = 0;
  105. }
  106. /* MDblock(MDp,X)
  107. ** Update message digest buffer MDp->buffer using 16-word data block X.
  108. ** Assumes all 16 words of X are full of data.
  109. ** Does not update MDp->count.
  110. ** This routine is not user-callable.
  111. */
  112. static void
  113. MDblock(MDp,Xb)
  114. MD4_CTX *MDp;
  115. unsigned char *Xb;
  116. {
  117. register unsigned int tmp, A, B, C, D;
  118. unsigned int X[16];
  119. int i;
  120. for (i = 0; i < 16; ++i) {
  121. X[i] = Xb[0] + (Xb[1] << 8) + (Xb[2] << 16) + (Xb[3] << 24);
  122. Xb += 4;
  123. }
  124. A = MDp->buffer[0];
  125. B = MDp->buffer[1];
  126. C = MDp->buffer[2];
  127. D = MDp->buffer[3];
  128. /* Update the message digest buffer */
  129. ff(A , B , C , D , 0 , fs1); /* Round 1 */
  130. ff(D , A , B , C , 1 , fs2);
  131. ff(C , D , A , B , 2 , fs3);
  132. ff(B , C , D , A , 3 , fs4);
  133. ff(A , B , C , D , 4 , fs1);
  134. ff(D , A , B , C , 5 , fs2);
  135. ff(C , D , A , B , 6 , fs3);
  136. ff(B , C , D , A , 7 , fs4);
  137. ff(A , B , C , D , 8 , fs1);
  138. ff(D , A , B , C , 9 , fs2);
  139. ff(C , D , A , B , 10 , fs3);
  140. ff(B , C , D , A , 11 , fs4);
  141. ff(A , B , C , D , 12 , fs1);
  142. ff(D , A , B , C , 13 , fs2);
  143. ff(C , D , A , B , 14 , fs3);
  144. ff(B , C , D , A , 15 , fs4);
  145. gg(A , B , C , D , 0 , gs1); /* Round 2 */
  146. gg(D , A , B , C , 4 , gs2);
  147. gg(C , D , A , B , 8 , gs3);
  148. gg(B , C , D , A , 12 , gs4);
  149. gg(A , B , C , D , 1 , gs1);
  150. gg(D , A , B , C , 5 , gs2);
  151. gg(C , D , A , B , 9 , gs3);
  152. gg(B , C , D , A , 13 , gs4);
  153. gg(A , B , C , D , 2 , gs1);
  154. gg(D , A , B , C , 6 , gs2);
  155. gg(C , D , A , B , 10 , gs3);
  156. gg(B , C , D , A , 14 , gs4);
  157. gg(A , B , C , D , 3 , gs1);
  158. gg(D , A , B , C , 7 , gs2);
  159. gg(C , D , A , B , 11 , gs3);
  160. gg(B , C , D , A , 15 , gs4);
  161. hh(A , B , C , D , 0 , hs1); /* Round 3 */
  162. hh(D , A , B , C , 8 , hs2);
  163. hh(C , D , A , B , 4 , hs3);
  164. hh(B , C , D , A , 12 , hs4);
  165. hh(A , B , C , D , 2 , hs1);
  166. hh(D , A , B , C , 10 , hs2);
  167. hh(C , D , A , B , 6 , hs3);
  168. hh(B , C , D , A , 14 , hs4);
  169. hh(A , B , C , D , 1 , hs1);
  170. hh(D , A , B , C , 9 , hs2);
  171. hh(C , D , A , B , 5 , hs3);
  172. hh(B , C , D , A , 13 , hs4);
  173. hh(A , B , C , D , 3 , hs1);
  174. hh(D , A , B , C , 11 , hs2);
  175. hh(C , D , A , B , 7 , hs3);
  176. hh(B , C , D , A , 15 , hs4);
  177. MDp->buffer[0] += A;
  178. MDp->buffer[1] += B;
  179. MDp->buffer[2] += C;
  180. MDp->buffer[3] += D;
  181. }
  182. /* MD4Update(MDp,X,count)
  183. ** Input: X -- a pointer to an array of unsigned characters.
  184. ** count -- the number of bits of X to use.
  185. ** (if not a multiple of 8, uses high bits of last byte.)
  186. ** Update MDp using the number of bits of X given by count.
  187. ** This is the basic input routine for an MD4 user.
  188. ** The routine completes the MD computation when count < 512, so
  189. ** every MD computation should end with one call to MD4Update with a
  190. ** count less than 512. A call with count 0 will be ignored if the
  191. ** MD has already been terminated (done != 0), so an extra call with
  192. ** count 0 can be given as a "courtesy close" to force termination
  193. ** if desired.
  194. */
  195. void
  196. MD4Update(MDp,X,count)
  197. MD4_CTX *MDp;
  198. unsigned char *X;
  199. unsigned int count;
  200. {
  201. unsigned int i, tmp, bit, byte, mask;
  202. unsigned char XX[64];
  203. unsigned char *p;
  204. /* return with no error if this is a courtesy close with count
  205. ** zero and MDp->done is true.
  206. */
  207. if (count == 0 && MDp->done) return;
  208. /* check to see if MD is already done and report error */
  209. if (MDp->done)
  210. { printf("\nError: MD4Update MD already done."); return; }
  211. /* Add count to MDp->count */
  212. tmp = count;
  213. p = MDp->count;
  214. while (tmp)
  215. { tmp += *p;
  216. *p++ = tmp;
  217. tmp = tmp >> 8;
  218. }
  219. /* Process data */
  220. if (count == 512)
  221. { /* Full block of data to handle */
  222. MDblock(MDp,X);
  223. }
  224. else if (count > 512) /* Check for count too large */
  225. {
  226. printf("\nError: MD4Update called with illegal count value %d.",
  227. count);
  228. return;
  229. }
  230. else /* partial block -- must be last block so finish up */
  231. {
  232. /* Find out how many bytes and residual bits there are */
  233. byte = count >> 3;
  234. bit = count & 7;
  235. /* Copy X into XX since we need to modify it */
  236. if (count)
  237. for (i=0;i<=byte;i++) XX[i] = X[i];
  238. for (i=byte+1;i<64;i++) 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. {
  245. for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
  246. MDblock(MDp,XX);
  247. }
  248. else /* need to do two blocks to finish up */
  249. {
  250. MDblock(MDp,XX);
  251. for (i=0;i<56;i++) XX[i] = 0;
  252. for (i=0;i<8;i++) XX[56+i] = MDp->count[i];
  253. MDblock(MDp,XX);
  254. }
  255. /* Set flag saying we're done with MD computation */
  256. MDp->done = 1;
  257. }
  258. }
  259. /*
  260. ** Finish up MD4 computation and return message digest.
  261. */
  262. void
  263. MD4Final(buf, MD)
  264. unsigned char *buf;
  265. MD4_CTX *MD;
  266. {
  267. int i, j;
  268. unsigned int w;
  269. MD4Update(MD, NULL, 0);
  270. for (i = 0; i < 4; ++i) {
  271. w = MD->buffer[i];
  272. for (j = 0; j < 4; ++j) {
  273. *buf++ = w;
  274. w >>= 8;
  275. }
  276. }
  277. }
  278. /*
  279. ** End of md4.c
  280. ****************************(cut)***********************************/