mdfour.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Unix SMB/Netbios implementation.
  3. * Version 1.9.
  4. * An implementation of MD4 designed for use in the SMB authentication protocol.
  5. *
  6. * Copyright (C) 1997-1998 Andrew Tridgell
  7. * Copyright (C) 2005-2008 Wayne Davison
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, visit the http://fsf.org website.
  21. */
  22. #include "rsync.h"
  23. /* NOTE: This code makes no attempt to be fast!
  24. *
  25. * It assumes that a int is at least 32 bits long. */
  26. static md_context *m;
  27. #define MASK32 (0xffffffff)
  28. #define F(X,Y,Z) ((((X)&(Y)) | ((~(X))&(Z))))
  29. #define G(X,Y,Z) ((((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z))))
  30. #define H(X,Y,Z) (((X)^(Y)^(Z)))
  31. #define lshift(x,s) (((((x)<<(s))&MASK32) | (((x)>>(32-(s)))&MASK32)))
  32. #define ROUND1(a,b,c,d,k,s) a = lshift((a + F(b,c,d) + M[k])&MASK32, s)
  33. #define ROUND2(a,b,c,d,k,s) a = lshift((a + G(b,c,d) + M[k] + 0x5A827999)&MASK32,s)
  34. #define ROUND3(a,b,c,d,k,s) a = lshift((a + H(b,c,d) + M[k] + 0x6ED9EBA1)&MASK32,s)
  35. /* this applies md4 to 64 byte chunks */
  36. static void mdfour64(uint32 *M)
  37. {
  38. uint32 AA, BB, CC, DD;
  39. uint32 A,B,C,D;
  40. A = m->A; B = m->B; C = m->C; D = m->D;
  41. AA = A; BB = B; CC = C; DD = D;
  42. ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7);
  43. ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19);
  44. ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7);
  45. ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19);
  46. ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7);
  47. ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19);
  48. ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7);
  49. ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19);
  50. ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5);
  51. ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13);
  52. ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5);
  53. ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13);
  54. ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5);
  55. ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13);
  56. ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5);
  57. ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13);
  58. ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9);
  59. ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15);
  60. ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9);
  61. ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15);
  62. ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9);
  63. ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15);
  64. ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9);
  65. ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15);
  66. A += AA; B += BB;
  67. C += CC; D += DD;
  68. A &= MASK32; B &= MASK32;
  69. C &= MASK32; D &= MASK32;
  70. m->A = A; m->B = B; m->C = C; m->D = D;
  71. }
  72. static void copy64(uint32 *M, const uchar *in)
  73. {
  74. int i;
  75. for (i = 0; i < MD4_DIGEST_LEN; i++) {
  76. M[i] = (in[i*4+3] << 24) | (in[i*4+2] << 16)
  77. | (in[i*4+1] << 8) | (in[i*4+0] << 0);
  78. }
  79. }
  80. static void copy4(uchar *out,uint32 x)
  81. {
  82. out[0] = x&0xFF;
  83. out[1] = (x>>8)&0xFF;
  84. out[2] = (x>>16)&0xFF;
  85. out[3] = (x>>24)&0xFF;
  86. }
  87. void mdfour_begin(md_context *md)
  88. {
  89. md->A = 0x67452301;
  90. md->B = 0xefcdab89;
  91. md->C = 0x98badcfe;
  92. md->D = 0x10325476;
  93. md->totalN = 0;
  94. md->totalN2 = 0;
  95. }
  96. static void mdfour_tail(const uchar *in, uint32 length)
  97. {
  98. uchar buf[128];
  99. uint32 M[16];
  100. extern int protocol_version;
  101. /*
  102. * Count total number of bits, modulo 2^64
  103. */
  104. m->totalN += length << 3;
  105. if (m->totalN < (length << 3))
  106. m->totalN2++;
  107. m->totalN2 += length >> 29;
  108. memset(buf, 0, 128);
  109. if (length)
  110. memcpy(buf, in, length);
  111. buf[length] = 0x80;
  112. if (length <= 55) {
  113. copy4(buf+56, m->totalN);
  114. /*
  115. * Prior to protocol version 27 only the number of bits
  116. * modulo 2^32 was included. MD4 requires the number
  117. * of bits modulo 2^64, which was fixed starting with
  118. * protocol version 27.
  119. */
  120. if (protocol_version >= 27)
  121. copy4(buf+60, m->totalN2);
  122. copy64(M, buf);
  123. mdfour64(M);
  124. } else {
  125. copy4(buf+120, m->totalN);
  126. /*
  127. * Prior to protocol version 27 only the number of bits
  128. * modulo 2^32 was included. MD4 requires the number
  129. * of bits modulo 2^64, which was fixed starting with
  130. * protocol version 27.
  131. */
  132. if (protocol_version >= 27)
  133. copy4(buf+124, m->totalN2);
  134. copy64(M, buf);
  135. mdfour64(M);
  136. copy64(M, buf+64);
  137. mdfour64(M);
  138. }
  139. }
  140. void mdfour_update(md_context *md, const uchar *in, uint32 length)
  141. {
  142. uint32 M[16];
  143. m = md;
  144. if (length == 0)
  145. mdfour_tail(in, length);
  146. while (length >= 64) {
  147. copy64(M, in);
  148. mdfour64(M);
  149. in += 64;
  150. length -= 64;
  151. m->totalN += 64 << 3;
  152. if (m->totalN < 64 << 3)
  153. m->totalN2++;
  154. }
  155. if (length)
  156. mdfour_tail(in, length);
  157. }
  158. void mdfour_result(md_context *md, uchar digest[MD4_DIGEST_LEN])
  159. {
  160. m = md;
  161. copy4(digest, m->A);
  162. copy4(digest+4, m->B);
  163. copy4(digest+8, m->C);
  164. copy4(digest+12, m->D);
  165. }
  166. void mdfour(uchar digest[MD4_DIGEST_LEN], uchar *in, int length)
  167. {
  168. md_context md;
  169. mdfour_begin(&md);
  170. mdfour_update(&md, in, length);
  171. mdfour_result(&md, digest);
  172. }
  173. #ifdef TEST_MDFOUR
  174. int protocol_version = 28;
  175. static void file_checksum1(char *fname)
  176. {
  177. int fd, i, was_multiple_of_64 = 1;
  178. md_context md;
  179. uchar buf[64*1024], sum[MD4_DIGEST_LEN];
  180. fd = open(fname,O_RDONLY);
  181. if (fd == -1) {
  182. perror("fname");
  183. exit(1);
  184. }
  185. mdfour_begin(&md);
  186. while (1) {
  187. int n = read(fd, buf, sizeof buf);
  188. if (n <= 0)
  189. break;
  190. was_multiple_of_64 = !(n % 64);
  191. mdfour_update(&md, buf, n);
  192. }
  193. if (was_multiple_of_64 && protocol_version >= 27)
  194. mdfour_update(&md, buf, 0);
  195. close(fd);
  196. mdfour_result(&md, sum);
  197. for (i = 0; i < MD4_DIGEST_LEN; i++)
  198. printf("%02X", sum[i]);
  199. printf("\n");
  200. }
  201. int main(int argc, char *argv[])
  202. {
  203. while (--argc)
  204. file_checksum1(*++argv);
  205. return 0;
  206. }
  207. #endif