md4.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. a implementation of MD4 designed for use in the SMB authentication protocol
  5. Copyright (C) Andrew Tridgell 1997-1998.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (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. See the
  13. 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 the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include "includes.h"
  19. /* NOTE: This code makes no attempt to be fast!
  20. It assumes that a int is at least 32 bits long
  21. */
  22. static uint32 A, B, C, D;
  23. static uint32 F(uint32 X, uint32 Y, uint32 Z)
  24. {
  25. return (X&Y) | ((~X)&Z);
  26. }
  27. static uint32 G(uint32 X, uint32 Y, uint32 Z)
  28. {
  29. return (X&Y) | (X&Z) | (Y&Z);
  30. }
  31. static uint32 H(uint32 X, uint32 Y, uint32 Z)
  32. {
  33. return X^Y^Z;
  34. }
  35. static uint32 lshift(uint32 x, int s)
  36. {
  37. x &= 0xFFFFFFFF;
  38. return ((x<<s)&0xFFFFFFFF) | (x>>(32-s));
  39. }
  40. #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
  41. #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + (uint32)0x5A827999,s)
  42. #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + (uint32)0x6ED9EBA1,s)
  43. /* this applies md4 to 64 byte chunks */
  44. static void mdfour64(uint32 *M)
  45. {
  46. int j;
  47. uint32 AA, BB, CC, DD;
  48. uint32 X[16];
  49. for (j=0;j<16;j++)
  50. X[j] = M[j];
  51. AA = A; BB = B; CC = C; DD = D;
  52. ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7);
  53. ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19);
  54. ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7);
  55. ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19);
  56. ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7);
  57. ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19);
  58. ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7);
  59. ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19);
  60. ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5);
  61. ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13);
  62. ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5);
  63. ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13);
  64. ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5);
  65. ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13);
  66. ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5);
  67. ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13);
  68. ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9);
  69. ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15);
  70. ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9);
  71. ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15);
  72. ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9);
  73. ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15);
  74. ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9);
  75. ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15);
  76. A += AA; B += BB; C += CC; D += DD;
  77. A &= 0xFFFFFFFF; B &= 0xFFFFFFFF;
  78. C &= 0xFFFFFFFF; D &= 0xFFFFFFFF;
  79. for (j=0;j<16;j++)
  80. X[j] = 0;
  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. /* produce a md4 message digest from data of length n bytes */
  97. void mdfour(unsigned char *out, unsigned char *in, int n)
  98. {
  99. unsigned char buf[128];
  100. uint32 M[16];
  101. uint32 b = n * 8;
  102. int i;
  103. A = 0x67452301;
  104. B = 0xefcdab89;
  105. C = 0x98badcfe;
  106. D = 0x10325476;
  107. while (n > 64) {
  108. copy64(M, in);
  109. mdfour64(M);
  110. in += 64;
  111. n -= 64;
  112. }
  113. for (i=0;i<128;i++)
  114. buf[i] = 0;
  115. memcpy(buf, in, n);
  116. buf[n] = 0x80;
  117. if (n <= 55) {
  118. copy4(buf+56, b);
  119. copy64(M, buf);
  120. mdfour64(M);
  121. } else {
  122. copy4(buf+120, b);
  123. copy64(M, buf);
  124. mdfour64(M);
  125. copy64(M, buf+64);
  126. mdfour64(M);
  127. }
  128. for (i=0;i<128;i++)
  129. buf[i] = 0;
  130. copy64(M, buf);
  131. copy4(out, A);
  132. copy4(out+4, B);
  133. copy4(out+8, C);
  134. copy4(out+12, D);
  135. A = B = C = D = 0;
  136. }