md4.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. {
  2. Translation of the SAMBA md4 code for FreePascal
  3. Copyright (C) 2006 by Ivo Steinmann
  4. Ported from SAMBA/source/lib/md4.c:
  5. }
  6. {
  7. Unix SMB/CIFS implementation.
  8. a implementation of MD4 designed for use in the SMB authentication protocol
  9. Copyright (C) Andrew Tridgell 1997-1998.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. }
  22. unit md4;
  23. {$mode objfpc}
  24. interface
  25. type
  26. TMD4Hash = packed array[0..15] of byte;
  27. // NOTE: _out is expected to be a 16 bytes array
  28. procedure mdfour({out} var _out: TMD4Hash; {in} _in: PByte; {in} n: Integer); overload;
  29. procedure mdfour({out} const _out: PByte; {in} _in: PByte; {in} n: Integer); overload;
  30. implementation
  31. procedure mdfour({out} var _out: TMD4Hash; {in} _in: PByte; {in} n: Integer);
  32. begin
  33. mdfour(@_out[0], _in, n);
  34. end;
  35. (*
  36. * produce a md4 message digest from data of length n bytes
  37. *)
  38. procedure mdfour({out} const _out: PByte; {in} _in: PByte; {in} n: Integer);
  39. var
  40. A, B, C, D: Longword;
  41. (* this applies md4 to 64 byte chunks *)
  42. procedure mdfour64({in} const M: PLongword);
  43. function F(X, Y, Z: Longword): Longword; inline;
  44. begin
  45. Result := (X and Y) or ((not X) and Z)
  46. end;
  47. function G(X, Y, Z: Longword): Longword; inline;
  48. begin
  49. Result := (X and Y) or (X and Z) or (Y and Z);
  50. end;
  51. function H(X, Y, Z: Longword): Longword; inline;
  52. begin
  53. Result := X xor Y xor Z;
  54. end;
  55. function lshift(X: Longword; S: Integer): Longword; inline;
  56. begin
  57. x := x and $FFFFFFFF;
  58. Result := ((x shl s) and $FFFFFFFF) or (x shr (32-s));
  59. end;
  60. procedure ROUND1(var a: Longword; b, c, d, k: Longword; s: Integer); inline;
  61. begin
  62. a := lshift(a + F(b,c,d) + M[k], s);
  63. end;
  64. procedure ROUND2(var a: Longword; b, c, d, k: Longword; s: Integer); inline;
  65. begin
  66. a := lshift(a + G(b,c,d) + M[k] + $5A827999, s);
  67. end;
  68. procedure ROUND3(var a: Longword; b, c, d, k: Longword; s: Integer); inline;
  69. begin
  70. a := lshift(a + H(b,c,d) + M[k] + $6ED9EBA1, s);
  71. end;
  72. var
  73. AA : Longword;
  74. BB : Longword;
  75. CC : Longword;
  76. DD : Longword;
  77. begin
  78. AA := A;
  79. BB := B;
  80. CC := C;
  81. DD := D;
  82. ROUND1(A,B,C,D, 0, 3); ROUND1(D,A,B,C, 1, 7);
  83. ROUND1(C,D,A,B, 2, 11); ROUND1(B,C,D,A, 3, 19);
  84. ROUND1(A,B,C,D, 4, 3); ROUND1(D,A,B,C, 5, 7);
  85. ROUND1(C,D,A,B, 6, 11); ROUND1(B,C,D,A, 7, 19);
  86. ROUND1(A,B,C,D, 8, 3); ROUND1(D,A,B,C, 9, 7);
  87. ROUND1(C,D,A,B, 10, 11); ROUND1(B,C,D,A, 11, 19);
  88. ROUND1(A,B,C,D, 12, 3); ROUND1(D,A,B,C, 13, 7);
  89. ROUND1(C,D,A,B, 14, 11); ROUND1(B,C,D,A, 15, 19);
  90. ROUND2(A,B,C,D, 0, 3); ROUND2(D,A,B,C, 4, 5);
  91. ROUND2(C,D,A,B, 8, 9); ROUND2(B,C,D,A, 12, 13);
  92. ROUND2(A,B,C,D, 1, 3); ROUND2(D,A,B,C, 5, 5);
  93. ROUND2(C,D,A,B, 9, 9); ROUND2(B,C,D,A, 13, 13);
  94. ROUND2(A,B,C,D, 2, 3); ROUND2(D,A,B,C, 6, 5);
  95. ROUND2(C,D,A,B, 10, 9); ROUND2(B,C,D,A, 14, 13);
  96. ROUND2(A,B,C,D, 3, 3); ROUND2(D,A,B,C, 7, 5);
  97. ROUND2(C,D,A,B, 11, 9); ROUND2(B,C,D,A, 15, 13);
  98. ROUND3(A,B,C,D, 0, 3); ROUND3(D,A,B,C, 8, 9);
  99. ROUND3(C,D,A,B, 4, 11); ROUND3(B,C,D,A, 12, 15);
  100. ROUND3(A,B,C,D, 2, 3); ROUND3(D,A,B,C, 10, 9);
  101. ROUND3(C,D,A,B, 6, 11); ROUND3(B,C,D,A, 14, 15);
  102. ROUND3(A,B,C,D, 1, 3); ROUND3(D,A,B,C, 9, 9);
  103. ROUND3(C,D,A,B, 5, 11); ROUND3(B,C,D,A, 13, 15);
  104. ROUND3(A,B,C,D, 3, 3); ROUND3(D,A,B,C, 11, 9);
  105. ROUND3(C,D,A,B, 7, 11); ROUND3(B,C,D,A, 15, 15);
  106. A := A + AA;
  107. B := B + BB;
  108. C := C + CC;
  109. D := D + DD;
  110. end;
  111. procedure copy64({out} const M: PLongword; {in} const _in: PByte);
  112. var
  113. i: Integer;
  114. begin
  115. for i := 0 to 15 do
  116. M[i] := (_in[i*4+3] shl 24) or (_in[i*4+2] shl 16) or (_in[i*4+1] shl 8) or (_in[i*4+0] shl 0);
  117. end;
  118. procedure copy4({out} const _out: PByte; {in} const x: Longword);
  119. begin
  120. _out[0] := x and $FF;
  121. _out[1] := (x shr 8) and $FF;
  122. _out[2] := (x shr 16) and $FF;
  123. _out[3] := (x shr 24) and $FF;
  124. end;
  125. var
  126. buf : array[0..127] of Byte;
  127. M : array[0..15] of Longword;
  128. bb : Longword;
  129. begin
  130. bb := n * 8;
  131. A := $67452301;
  132. B := $efcdab89;
  133. C := $98badcfe;
  134. D := $10325476;
  135. while n > 64 do
  136. begin
  137. copy64(@M[0], _in);
  138. mdfour64(@M[0]);
  139. Inc(_in, 64);
  140. Dec(n, 64);
  141. end;
  142. FillChar(buf, sizeof(buf), 0);
  143. Move(_in[0], buf, n);
  144. buf[n] := $80;
  145. if n <= 55 then
  146. begin
  147. copy4(@buf[56], bb);
  148. copy64(@M[0], @buf[0]);
  149. mdfour64(@M[0]);
  150. end else begin
  151. copy4(@buf[120], bb);
  152. copy64(@M[0], @buf[0]);
  153. mdfour64(@M[0]);
  154. copy64(@M[0], @buf[64]);
  155. mdfour64(@M[0]);
  156. end;
  157. FillChar(buf, sizeof(buf), 0);
  158. copy64(@M[0], @buf[0]);
  159. copy4(@_out[0], A);
  160. copy4(@_out[4], B);
  161. copy4(@_out[8], C);
  162. copy4(@_out[12], D);
  163. end;
  164. end.