md4.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. Modified by Steve French (sfrench@us.ibm.com) 2002-2003
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include "cifsencrypt.h"
  22. /* NOTE: This code makes no attempt to be fast! */
  23. static __u32
  24. F(__u32 X, __u32 Y, __u32 Z)
  25. {
  26. return (X & Y) | ((~X) & Z);
  27. }
  28. static __u32
  29. G(__u32 X, __u32 Y, __u32 Z)
  30. {
  31. return (X & Y) | (X & Z) | (Y & Z);
  32. }
  33. static __u32
  34. H(__u32 X, __u32 Y, __u32 Z)
  35. {
  36. return X ^ Y ^ Z;
  37. }
  38. static __u32
  39. lshift(__u32 x, int s)
  40. {
  41. x &= 0xFFFFFFFF;
  42. return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
  43. }
  44. #define ROUND1(a,b,c,d,k,s) (*a) = lshift((*a) + F(*b,*c,*d) + X[k], s)
  45. #define ROUND2(a,b,c,d,k,s) (*a) = lshift((*a) + G(*b,*c,*d) + X[k] + (__u32)0x5A827999,s)
  46. #define ROUND3(a,b,c,d,k,s) (*a) = lshift((*a) + H(*b,*c,*d) + X[k] + (__u32)0x6ED9EBA1,s)
  47. /* this applies md4 to 64 byte chunks */
  48. static void
  49. mdfour64(__u32 * M, __u32 * A, __u32 *B, __u32 * C, __u32 *D)
  50. {
  51. int j;
  52. __u32 AA, BB, CC, DD;
  53. __u32 X[16];
  54. for (j = 0; j < 16; j++)
  55. X[j] = M[j];
  56. AA = *A;
  57. BB = *B;
  58. CC = *C;
  59. DD = *D;
  60. ROUND1(A, B, C, D, 0, 3);
  61. ROUND1(D, A, B, C, 1, 7);
  62. ROUND1(C, D, A, B, 2, 11);
  63. ROUND1(B, C, D, A, 3, 19);
  64. ROUND1(A, B, C, D, 4, 3);
  65. ROUND1(D, A, B, C, 5, 7);
  66. ROUND1(C, D, A, B, 6, 11);
  67. ROUND1(B, C, D, A, 7, 19);
  68. ROUND1(A, B, C, D, 8, 3);
  69. ROUND1(D, A, B, C, 9, 7);
  70. ROUND1(C, D, A, B, 10, 11);
  71. ROUND1(B, C, D, A, 11, 19);
  72. ROUND1(A, B, C, D, 12, 3);
  73. ROUND1(D, A, B, C, 13, 7);
  74. ROUND1(C, D, A, B, 14, 11);
  75. ROUND1(B, C, D, A, 15, 19);
  76. ROUND2(A, B, C, D, 0, 3);
  77. ROUND2(D, A, B, C, 4, 5);
  78. ROUND2(C, D, A, B, 8, 9);
  79. ROUND2(B, C, D, A, 12, 13);
  80. ROUND2(A, B, C, D, 1, 3);
  81. ROUND2(D, A, B, C, 5, 5);
  82. ROUND2(C, D, A, B, 9, 9);
  83. ROUND2(B, C, D, A, 13, 13);
  84. ROUND2(A, B, C, D, 2, 3);
  85. ROUND2(D, A, B, C, 6, 5);
  86. ROUND2(C, D, A, B, 10, 9);
  87. ROUND2(B, C, D, A, 14, 13);
  88. ROUND2(A, B, C, D, 3, 3);
  89. ROUND2(D, A, B, C, 7, 5);
  90. ROUND2(C, D, A, B, 11, 9);
  91. ROUND2(B, C, D, A, 15, 13);
  92. ROUND3(A, B, C, D, 0, 3);
  93. ROUND3(D, A, B, C, 8, 9);
  94. ROUND3(C, D, A, B, 4, 11);
  95. ROUND3(B, C, D, A, 12, 15);
  96. ROUND3(A, B, C, D, 2, 3);
  97. ROUND3(D, A, B, C, 10, 9);
  98. ROUND3(C, D, A, B, 6, 11);
  99. ROUND3(B, C, D, A, 14, 15);
  100. ROUND3(A, B, C, D, 1, 3);
  101. ROUND3(D, A, B, C, 9, 9);
  102. ROUND3(C, D, A, B, 5, 11);
  103. ROUND3(B, C, D, A, 13, 15);
  104. ROUND3(A, B, C, D, 3, 3);
  105. ROUND3(D, A, B, C, 11, 9);
  106. ROUND3(C, D, A, B, 7, 11);
  107. ROUND3(B, C, D, A, 15, 15);
  108. *A += AA;
  109. *B += BB;
  110. *C += CC;
  111. *D += DD;
  112. *A &= 0xFFFFFFFF;
  113. *B &= 0xFFFFFFFF;
  114. *C &= 0xFFFFFFFF;
  115. *D &= 0xFFFFFFFF;
  116. for (j = 0; j < 16; j++)
  117. X[j] = 0;
  118. }
  119. static void
  120. copy64(__u32 * M, unsigned char *in)
  121. {
  122. int i;
  123. for (i = 0; i < 16; i++)
  124. M[i] = (in[i * 4 + 3] << 24) | (in[i * 4 + 2] << 16) |
  125. (in[i * 4 + 1] << 8) | (in[i * 4 + 0] << 0);
  126. }
  127. static void
  128. copy4(unsigned char *out, __u32 x)
  129. {
  130. out[0] = x & 0xFF;
  131. out[1] = (x >> 8) & 0xFF;
  132. out[2] = (x >> 16) & 0xFF;
  133. out[3] = (x >> 24) & 0xFF;
  134. }
  135. /* produce a md4 message digest from data of length n bytes */
  136. void
  137. mdfour(unsigned char *out, unsigned char *in, int n)
  138. {
  139. unsigned char buf[128];
  140. __u32 M[16];
  141. __u32 b = n * 8;
  142. int i;
  143. __u32 A = 0x67452301;
  144. __u32 B = 0xefcdab89;
  145. __u32 C = 0x98badcfe;
  146. __u32 D = 0x10325476;
  147. while (n > 64) {
  148. copy64(M, in);
  149. mdfour64(M,&A,&B, &C, &D);
  150. in += 64;
  151. n -= 64;
  152. }
  153. for (i = 0; i < 128; i++)
  154. buf[i] = 0;
  155. memcpy(buf, in, n);
  156. buf[n] = 0x80;
  157. if (n <= 55) {
  158. copy4(buf + 56, b);
  159. copy64(M, buf);
  160. mdfour64(M, &A, &B, &C, &D);
  161. } else {
  162. copy4(buf + 120, b);
  163. copy64(M, buf);
  164. mdfour64(M, &A, &B, &C, &D);
  165. copy64(M, buf + 64);
  166. mdfour64(M, &A, &B, &C, &D);
  167. }
  168. for (i = 0; i < 128; i++)
  169. buf[i] = 0;
  170. copy64(M, buf);
  171. copy4(out, A);
  172. copy4(out + 4, B);
  173. copy4(out + 8, C);
  174. copy4(out + 12, D);
  175. A = B = C = D = 0;
  176. }