mdfour.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. mdfour.c
  3. An implementation of MD4 designed for use in the samba SMB
  4. authentication protocol
  5. Copyright (C) 1997-1998 Andrew Tridgell
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. See the GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to:
  16. Free Software Foundation, Inc.
  17. 59 Temple Place - Suite 330
  18. Boston, MA 02111-1307, USA
  19. $Id: mdfour.c,v 1.1 2002/08/23 22:03:27 abster Exp $
  20. */
  21. #include <string.h> /* XoXus: needed for memset call */
  22. #include "mdfour.h"
  23. /* NOTE: This code makes no attempt to be fast!
  24. It assumes that a int is at least 32 bits long
  25. */
  26. static struct mdfour *m;
  27. #define F(X,Y,Z) (((X)&(Y)) | ((~(X))&(Z)))
  28. #define G(X,Y,Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
  29. #define H(X,Y,Z) ((X)^(Y)^(Z))
  30. #ifdef LARGE_INT32
  31. #define lshift(x,s) ((((x)<<(s))&0xFFFFFFFF) | (((x)>>(32-(s)))&0xFFFFFFFF))
  32. #else
  33. #define lshift(x,s) (((x)<<(s)) | ((x)>>(32-(s))))
  34. #endif
  35. #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
  36. #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + 0x5A827999,s)
  37. #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s)
  38. /* this applies md4 to 64 byte chunks */
  39. static void mdfour64(uint32 *M)
  40. {
  41. int j;
  42. uint32 AA, BB, CC, DD;
  43. uint32 X[16];
  44. uint32 A,B,C,D;
  45. for (j=0;j<16;j++)
  46. X[j] = M[j];
  47. A = m->A; B = m->B; C = m->C; D = m->D;
  48. AA = A; BB = B; CC = C; DD = D;
  49. ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7);
  50. ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19);
  51. ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7);
  52. ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19);
  53. ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7);
  54. ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19);
  55. ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7);
  56. ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19);
  57. ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5);
  58. ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13);
  59. ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5);
  60. ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13);
  61. ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5);
  62. ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13);
  63. ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5);
  64. ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13);
  65. ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9);
  66. ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15);
  67. ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9);
  68. ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15);
  69. ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9);
  70. ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15);
  71. ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9);
  72. ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15);
  73. A += AA; B += BB; C += CC; D += DD;
  74. #ifdef LARGE_INT32
  75. A &= 0xFFFFFFFF; B &= 0xFFFFFFFF;
  76. C &= 0xFFFFFFFF; D &= 0xFFFFFFFF;
  77. #endif
  78. for (j=0;j<16;j++)
  79. X[j] = 0;
  80. m->A = A; m->B = B; m->C = C; m->D = D;
  81. }
  82. static void copy64(uint32 *M, unsigned char *in)
  83. {
  84. int i;
  85. for (i=0;i<16;i++)
  86. M[i] = (in[i*4+3]<<24) | (in[i*4+2]<<16) |
  87. (in[i*4+1]<<8) | (in[i*4+0]<<0);
  88. }
  89. static void copy4(unsigned char *out,uint32 x)
  90. {
  91. out[0] = x&0xFF;
  92. out[1] = (x>>8)&0xFF;
  93. out[2] = (x>>16)&0xFF;
  94. out[3] = (x>>24)&0xFF;
  95. }
  96. void mdfour_begin(struct mdfour *md)
  97. {
  98. md->A = 0x67452301;
  99. md->B = 0xefcdab89;
  100. md->C = 0x98badcfe;
  101. md->D = 0x10325476;
  102. md->totalN = 0;
  103. }
  104. static void mdfour_tail(unsigned char *in, int n)
  105. {
  106. unsigned char buf[128];
  107. uint32 M[16];
  108. uint32 b;
  109. m->totalN += n;
  110. b = m->totalN * 8;
  111. memset(buf, 0, 128);
  112. if (n) memcpy(buf, in, n);
  113. buf[n] = 0x80;
  114. if (n <= 55) {
  115. copy4(buf+56, b);
  116. copy64(M, buf);
  117. mdfour64(M);
  118. } else {
  119. copy4(buf+120, b);
  120. copy64(M, buf);
  121. mdfour64(M);
  122. copy64(M, buf+64);
  123. mdfour64(M);
  124. }
  125. }
  126. void mdfour_update(struct mdfour *md, unsigned char *in, int n)
  127. {
  128. uint32 M[16];
  129. if (n == 0) mdfour_tail(in, n);
  130. m = md;
  131. while (n >= 64) {
  132. copy64(M, in);
  133. mdfour64(M);
  134. in += 64;
  135. n -= 64;
  136. m->totalN += 64;
  137. }
  138. mdfour_tail(in, n);
  139. }
  140. void mdfour_result(struct mdfour *md, unsigned char *out)
  141. {
  142. m = md;
  143. copy4(out, m->A);
  144. copy4(out+4, m->B);
  145. copy4(out+8, m->C);
  146. copy4(out+12, m->D);
  147. }
  148. void mdfour(unsigned char *out, unsigned char *in, int n)
  149. {
  150. struct mdfour md;
  151. mdfour_begin(&md);
  152. mdfour_update(&md, in, n);
  153. mdfour_result(&md, out);
  154. }
  155. ///////////////////////////////////////////////////////////////
  156. // MD4-based checksum utility functions
  157. //
  158. // Copyright (C) 2000 Jeff Teunissen <d2deek@pmail.net>
  159. //
  160. // Author: Jeff Teunissen <d2deek@pmail.net>
  161. // Date: 01 Jan 2000
  162. unsigned Com_BlockChecksum (void *buffer, int length)
  163. {
  164. int digest[4];
  165. unsigned val;
  166. mdfour ( (unsigned char *) digest, (unsigned char *) buffer, length );
  167. val = digest[0] ^ digest[1] ^ digest[2] ^ digest[3];
  168. return val;
  169. }
  170. void Com_BlockFullChecksum (void *buffer, int len, unsigned char *outbuf)
  171. {
  172. mdfour ( outbuf, (unsigned char *) buffer, len );
  173. }