OverbyteIcsMD4.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. {*_* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. Author: François PIETTE
  3. Creation: Sep 03, 2004
  4. Version: 1.00
  5. Description: MD4 is an implementation of the MD4 Message-Digest Algorithm
  6. as described in RFC1320
  7. Credit: This unit is based on code written by David Barton
  8. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9. Copyright (c) 1999-2002 David Barton (crypto@cityinthesky.co.uk)
  10. Permission is hereby granted, free of charge, to any person obtaining a
  11. copy of this software and associated documentation files (the "Software"),
  12. to deal in the Software without restriction, including without limitation
  13. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. and/or sell copies of the Software, and to permit persons to whom the
  15. Software is furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  19. EMail: francois.piette@overbyte.be http://www.overbyte.be
  20. Support: Use the mailing list twsocket@elists.org
  21. Follow "support" link at http://www.overbyte.be for subscription.
  22. Legal issues: Copyright (C) 2004-2006 by François PIETTE
  23. Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
  24. <francois.piette@overbyte.be>
  25. This software is provided 'as-is', without any express or
  26. implied warranty. In no event will the author be held liable
  27. for any damages arising from the use of this software.
  28. Permission is granted to anyone to use this software for any
  29. purpose, including commercial applications, and to alter it
  30. and redistribute it freely, subject to the following
  31. restrictions:
  32. 1. The origin of this software must not be misrepresented,
  33. you must not claim that you wrote the original software.
  34. If you use this software in a product, an acknowledgment
  35. in the product documentation would be appreciated but is
  36. not required.
  37. 2. Altered source versions must be plainly marked as such, and
  38. must not be misrepresented as being the original software.
  39. 3. This notice may not be removed or altered from any source
  40. distribution.
  41. 4. You must register this software by sending a picture postcard
  42. to the author. Use a nice stamp and mention your name, street
  43. address, EMail address and any comment you like to say.
  44. History:
  45. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  46. unit OverbyteIcsMD4;
  47. {$I OverbyteIcsDefs.inc}
  48. interface
  49. uses
  50. Windows, Classes, Sysutils{, DCPcrypt2, DCPconst};
  51. const
  52. IcsMD4Version = 100;
  53. CopyRight : String = ' IcsMD4 (c) 2004-2006 F. Piette V1.00 ';
  54. type
  55. {$IFDEF DELPHI3}
  56. LongWord = DWORD;
  57. {$ENDIF}
  58. MD4Exception = class(Exception);
  59. TMD4Digest = array [0..15] of Byte;
  60. PMD4Digest = ^TMD4Digest;
  61. TMD4Context = record
  62. FInitialized : Boolean; { Whether or not the algorithm has been initialized }
  63. LenHi, LenLo : LongWord;
  64. Index : DWord;
  65. CurrentHash : array [0..3] of DWord;
  66. HashBuffer : array [0..63] of Byte;
  67. end;
  68. function MD4String(const Value : String) : String;
  69. procedure MD4Init(var MD4Context : TMD4Context);
  70. procedure MD4Burn(var MD4Context : TMD4Context);
  71. procedure MD4Update(var MD4Context : TMD4Context; const Buffer; Size: LongWord);
  72. procedure MD4UpdateStr(var MD4Context : TMD4Context; const Str: String);
  73. procedure MD4Compress(var MD4Context : TMD4Context);
  74. procedure MD4Final(var MD4Context : TMD4Context; var Digest : TMD4Digest);
  75. implementation
  76. {$R-}{$Q-}
  77. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  78. function LRot32(a, b: LongWord): LongWord;
  79. begin
  80. Result:= (a shl b) or (a shr (32-b));
  81. end;
  82. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  83. procedure MD4Burn(var MD4Context : TMD4Context);
  84. begin
  85. MD4Context.LenHi:= 0;
  86. MD4Context.LenLo:= 0;
  87. MD4Context.Index:= 0;
  88. FillChar(MD4Context.HashBuffer, Sizeof(MD4Context.HashBuffer), 0);
  89. FillChar(MD4Context.CurrentHash, Sizeof(MD4Context.CurrentHash), 0);
  90. MD4Context.FInitialized := FALSE;
  91. end;
  92. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  93. procedure MD4Init(var MD4Context : TMD4Context);
  94. begin
  95. MD4Burn(MD4Context);
  96. MD4Context.CurrentHash[0] := $67452301;
  97. MD4Context.CurrentHash[1] := $efcdab89;
  98. MD4Context.CurrentHash[2] := $98badcfe;
  99. MD4Context.CurrentHash[3] := $10325476;
  100. MD4Context.FInitialized := TRUE;
  101. end;
  102. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  103. procedure MD4Update(var MD4Context : TMD4Context; const Buffer; Size: LongWord);
  104. var
  105. PBuf: ^Byte;
  106. begin
  107. if not MD4Context.FInitialized then
  108. raise MD4Exception.Create('MD4 not initialized');
  109. Inc(MD4Context.LenHi, Size shr 29);
  110. Inc(MD4Context.LenLo, Size * 8);
  111. if MD4Context.LenLo < (Size * 8) then
  112. Inc(MD4Context.LenHi);
  113. PBuf:= @Buffer;
  114. while Size > 0 do begin
  115. if (Sizeof(MD4Context.HashBuffer) - MD4Context.Index) <= DWord(Size) then begin
  116. Move(PBuf^, MD4Context.HashBuffer[MD4Context.Index], Sizeof(MD4Context.HashBuffer) - MD4Context.Index);
  117. Dec(Size, Sizeof(MD4Context.HashBuffer) - MD4Context.Index);
  118. Inc(PBuf, Sizeof(MD4Context.HashBuffer) - MD4Context.Index);
  119. MD4Compress(MD4Context);
  120. end
  121. else begin
  122. Move(PBuf^, MD4Context.HashBuffer[MD4Context.Index], Size);
  123. Inc(MD4Context.Index, Size);
  124. Size := 0;
  125. end;
  126. end;
  127. end;
  128. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  129. procedure MD4Compress(var MD4Context : TMD4Context);
  130. var
  131. Data : array [0..15] of DWord;
  132. A, B, C, D: DWord;
  133. begin
  134. Move(MD4Context.HashBuffer, Data, Sizeof(Data));
  135. A:= MD4Context.CurrentHash[0];
  136. B:= MD4Context.CurrentHash[1];
  137. C:= MD4Context.CurrentHash[2];
  138. D:= MD4Context.CurrentHash[3];
  139. A:= LRot32(A + (D xor (B and (C xor D))) + Data[ 0], 3);
  140. D:= LRot32(D + (C xor (A and (B xor C))) + Data[ 1], 7);
  141. C:= LRot32(C + (B xor (D and (A xor B))) + Data[ 2], 11);
  142. B:= LRot32(B + (A xor (C and (D xor A))) + Data[ 3], 19);
  143. A:= LRot32(A + (D xor (B and (C xor D))) + Data[ 4], 3);
  144. D:= LRot32(D + (C xor (A and (B xor C))) + Data[ 5], 7);
  145. C:= LRot32(C + (B xor (D and (A xor B))) + Data[ 6], 11);
  146. B:= LRot32(B + (A xor (C and (D xor A))) + Data[ 7], 19);
  147. A:= LRot32(A + (D xor (B and (C xor D))) + Data[ 8], 3);
  148. D:= LRot32(D + (C xor (A and (B xor C))) + Data[ 9], 7);
  149. C:= LRot32(C + (B xor (D and (A xor B))) + Data[10], 11);
  150. B:= LRot32(B + (A xor (C and (D xor A))) + Data[11], 19);
  151. A:= LRot32(A + (D xor (B and (C xor D))) + Data[12], 3);
  152. D:= LRot32(D + (C xor (A and (B xor C))) + Data[13], 7);
  153. C:= LRot32(C + (B xor (D and (A xor B))) + Data[14], 11);
  154. B:= LRot32(B + (A xor (C and (D xor A))) + Data[15], 19);
  155. A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 0] + $5a827999, 3);
  156. D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 4] + $5a827999, 5);
  157. C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[ 8] + $5a827999, 9);
  158. B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[12] + $5a827999, 13);
  159. A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 1] + $5a827999, 3);
  160. D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 5] + $5a827999, 5);
  161. C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[ 9] + $5a827999, 9);
  162. B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[13] + $5a827999, 13);
  163. A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 2] + $5a827999, 3);
  164. D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 6] + $5a827999, 5);
  165. C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[10] + $5a827999, 9);
  166. B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[14] + $5a827999, 13);
  167. A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 3] + $5a827999, 3);
  168. D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 7] + $5a827999, 5);
  169. C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[11] + $5a827999, 9);
  170. B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[15] + $5a827999, 13);
  171. A:= LRot32(A + (B xor C xor D) + Data[ 0] + $6ed9eba1, 3);
  172. D:= LRot32(D + (A xor B xor C) + Data[ 8] + $6ed9eba1, 9);
  173. C:= LRot32(C + (D xor A xor B) + Data[ 4] + $6ed9eba1, 11);
  174. B:= LRot32(B + (C xor D xor A) + Data[12] + $6ed9eba1, 15);
  175. A:= LRot32(A + (B xor C xor D) + Data[ 2] + $6ed9eba1, 3);
  176. D:= LRot32(D + (A xor B xor C) + Data[10] + $6ed9eba1, 9);
  177. C:= LRot32(C + (D xor A xor B) + Data[ 6] + $6ed9eba1, 11);
  178. B:= LRot32(B + (C xor D xor A) + Data[14] + $6ed9eba1, 15);
  179. A:= LRot32(A + (B xor C xor D) + Data[ 1] + $6ed9eba1, 3);
  180. D:= LRot32(D + (A xor B xor C) + Data[ 9] + $6ed9eba1, 9);
  181. C:= LRot32(C + (D xor A xor B) + Data[ 5] + $6ed9eba1, 11);
  182. B:= LRot32(B + (C xor D xor A) + Data[13] + $6ed9eba1, 15);
  183. A:= LRot32(A + (B xor C xor D) + Data[ 3] + $6ed9eba1, 3);
  184. D:= LRot32(D + (A xor B xor C) + Data[11] + $6ed9eba1, 9);
  185. C:= LRot32(C + (D xor A xor B) + Data[ 7] + $6ed9eba1, 11);
  186. B:= LRot32(B + (C xor D xor A) + Data[15] + $6ed9eba1, 15);
  187. Inc(MD4Context.CurrentHash[0], A);
  188. Inc(MD4Context.CurrentHash[1], B);
  189. Inc(MD4Context.CurrentHash[2], C);
  190. Inc(MD4Context.CurrentHash[3], D);
  191. MD4Context.Index:= 0;
  192. FillChar(MD4Context.HashBuffer, Sizeof(MD4Context.HashBuffer), 0);
  193. end;
  194. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  195. procedure MD4Final(var MD4Context : TMD4Context; var Digest : TMD4Digest);
  196. begin
  197. if not MD4Context.FInitialized then
  198. raise MD4Exception.Create('MD4 not initialized');
  199. MD4Context.HashBuffer[MD4Context.Index] := $80;
  200. if MD4Context.Index >= 56 then
  201. MD4Compress(MD4Context);
  202. PDWord(@(MD4Context.HashBuffer[56]))^ := MD4Context.LenLo;
  203. PDWord(@(MD4Context.HashBuffer[60]))^ := MD4Context.LenHi;
  204. MD4Compress(MD4Context);
  205. Move(MD4Context.CurrentHash, Digest, Sizeof(MD4Context.CurrentHash));
  206. MD4Burn(MD4Context);
  207. end;
  208. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  209. procedure MD4UpdateStr(var MD4Context : TMD4Context; const Str: String);
  210. begin
  211. if Str = '' then
  212. MD4Update(MD4Context, PChar(0)^, 0)
  213. else
  214. MD4Update(MD4Context, Str[1], Length(Str));
  215. end;
  216. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  217. function MD4String(const Value : String) : String;
  218. var
  219. MD4Context: TMD4Context;
  220. begin
  221. MD4Init(MD4Context);
  222. MD4UpdateStr(MD4Context, Value);
  223. SetLength(Result, 16);
  224. MD4Final(MD4Context, PMD4Digest(@Result[1])^);
  225. end;
  226. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  227. {$IFDEF SELFTEST}
  228. function MD4SelfTest : Boolean;
  229. const
  230. Test1Out: TMD4Digest =
  231. ($a4,$48,$01,$7a,$af,$21,$d8,$52,$5f,$c1,$0a,$e8,$7a,$a6,$72,$9d);
  232. Test2Out: TMD4Digest =
  233. ($d7,$9e,$1c,$30,$8a,$a5,$bb,$cd,$ee,$a8,$ed,$63,$df,$41,$2d,$a9);
  234. var
  235. MD4Context : TMD4Context;
  236. TestOut : TMD4Digest;
  237. begin
  238. MD4Init(MD4Context);
  239. MD4UpdateStr(MD4Context, 'abc');
  240. MD4Final(MD4Context, TestOut);
  241. Result:= CompareMem(@TestOut, @Test1Out, Sizeof(Test1Out));
  242. MD4Init(MD4Context);
  243. MD4UpdateStr(MD4Context, 'abcdefghijklmnopqrstuvwxyz');
  244. MD4Final(MD4Context, TestOut);
  245. Result:= CompareMem(@TestOut, @Test2Out, Sizeof(Test2Out)) and Result;
  246. end;
  247. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  248. initialization
  249. MD4SelfTest;
  250. {$ENDIF}
  251. end.