IdHashSHA.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.6 2003-10-12 15:25:50 HHellström
  18. Comments added
  19. Rev 1.5 2003-10-12 03:08:24 HHellström
  20. New implementation; copyright changed. The source code formatting has been
  21. adjusted to fit the margins. The new implementation is faster on dotNet
  22. compared to the old one, but is slightly slower on Win32.
  23. Rev 1.4 2003-10-11 18:44:54 HHellström
  24. Range checking and overflow checking disabled in the Coder method only. The
  25. purpose of this setting is to force the arithmetic operations performed on
  26. LongWord variables to be modulo $100000000. This hack entails reasonable
  27. performance on both Win32 and dotNet.
  28. Rev 1.3 10/10/2003 2:20:56 PM GGrieve
  29. turn range checking off
  30. Rev 1.2 2003-09-21 17:31:02 HHellström Version: 1.2
  31. DotNET compatibility
  32. Rev 1.1 2/16/2003 03:19:18 PM JPMugaas
  33. Should now compile on D7 better.
  34. Rev 1.0 11/13/2002 07:53:48 AM JPMugaas
  35. }
  36. unit IdHashSHA;
  37. interface
  38. {$i IdCompilerDefines.inc}
  39. uses
  40. Classes,
  41. IdFIPS,
  42. IdGlobal, IdHash;
  43. type
  44. T5x4LongWordRecord = array[0..4] of UInt32;
  45. T512BitRecord = array [0..63] of Byte;
  46. TIdHashSHA1 = class(TIdHashNativeAndIntF)
  47. protected
  48. FCheckSum: T5x4LongWordRecord;
  49. FCBuffer: TIdBytes;
  50. procedure Coder;
  51. function NativeGetHashBytes(AStream: TStream; ASize: Int64): TIdBytes; override;
  52. function HashToHex(const AHash: TIdBytes): String; override;
  53. function InitHash : TIdHashIntCtx; override;
  54. public
  55. constructor Create; override;
  56. class function IsAvailable : Boolean; override;
  57. class function IsIntfAvailable: Boolean; override;
  58. end;
  59. TIdHashSHA224 = class(TIdHashIntF)
  60. protected
  61. function InitHash : TIdHashIntCtx; override;
  62. public
  63. class function IsAvailable : Boolean; override;
  64. end;
  65. TIdHashSHA256 = class(TIdHashIntF)
  66. protected
  67. function InitHash : TIdHashIntCtx; override;
  68. public
  69. class function IsAvailable : Boolean; override;
  70. end;
  71. TIdHashSHA384 = class(TIdHashIntF)
  72. protected
  73. function InitHash : TIdHashIntCtx; override;
  74. public
  75. class function IsAvailable : Boolean; override;
  76. end;
  77. TIdHashSHA512 = class(TIdHashIntF)
  78. protected
  79. function InitHash : TIdHashIntCtx; override;
  80. public
  81. class function IsAvailable : Boolean; override;
  82. end;
  83. implementation
  84. { TIdHashSHA1 }
  85. function SwapUInt32(const AValue: UInt32): UInt32;
  86. begin
  87. Result := ((AValue and $FF) shl 24) or ((AValue and $FF00) shl 8) or ((AValue and $FF0000) shr 8) or ((AValue and $FF000000) shr 24);
  88. end;
  89. constructor TIdHashSHA1.Create;
  90. begin
  91. inherited Create;
  92. SetLength(FCBuffer, 64);
  93. end;
  94. function TIdHashSHA1.InitHash: TIdHashIntCtx;
  95. begin
  96. Result := GetSHA1HashInst;
  97. end;
  98. class function TIdHashSHA1.IsIntfAvailable: Boolean;
  99. begin
  100. Result := IsHashingIntfAvail and IsSHA1HashIntfAvail;
  101. end;
  102. {$I IdOverflowCheckingOff.inc} // Operations performed modulo $100000000
  103. {$I IdRangeCheckingOff.inc}
  104. procedure TIdHashSHA1.Coder;
  105. var
  106. T, A, B, C, D, E: UInt32;
  107. { The size of the W variable has been reduced to make the Coder method
  108. consume less memory on dotNet. This change has been tested with the v1.1
  109. framework and entails a general increase of performance by >50%. }
  110. W: array [0..19] of UInt32;
  111. i: UInt32;
  112. begin
  113. { The first 16 W values are identical to the input block with endian
  114. conversion. }
  115. for i := 0 to 15 do
  116. begin
  117. W[i]:= (FCBuffer[i*4] shl 24) or
  118. (FCBuffer[i*4+1] shl 16) or
  119. (FCBuffer[i*4+2] shl 8) or
  120. FCBuffer[i*4+3];
  121. end;
  122. { In normal x86 code all of the remaining 64 W values would be calculated
  123. here. Here only the four next values are calculated, to reduce the code
  124. size of the first of the four loops below. }
  125. for i := 16 to 19 do
  126. begin
  127. T := W[i-3] xor W[i-8] xor W[i-14] xor W[i-16];
  128. W[i] := (T shl 1) or (T shr 31);
  129. end;
  130. A := FCheckSum[0];
  131. B := FCheckSum[1];
  132. C := FCheckSum[2];
  133. D := FCheckSum[3];
  134. E := FCheckSum[4];
  135. { The following loop could be expanded, but has been kept together to reduce
  136. the code size. A small code size entails better performance due to CPU
  137. caching.
  138. Note that the code size could be reduced further by using the SHA-1
  139. reference code:
  140. for i := 0 to 19 do begin
  141. T := E + (A shl 5) + (A shr 27) + (D xor (B and (C xor D))) + W[i];
  142. Inc(T,$5A827999);
  143. E := D;
  144. D := C;
  145. C := (B shl 30) + (B shr 2);
  146. B := A;
  147. A := T;
  148. end;
  149. The reference code is usually (at least partly) expanded, mostly because
  150. the assignments that circle the state variables A, B, C, D and E are costly,
  151. in particular on dotNET. (In x86 code further optimization can be achieved
  152. by eliminating the loop variable, which occupies a CPU register that is
  153. better used by one of the state variables, plus by expanding the W array
  154. at the beginning.) }
  155. i := 0;
  156. repeat
  157. Inc(E,(A shl 5) + (A shr 27) + (D xor (B and (C xor D))) + W[i+0]);
  158. Inc(E,$5A827999);
  159. B := (B shl 30) + (B shr 2);
  160. Inc(D,(E shl 5) + (E shr 27) + (C xor (A and (B xor C))) + W[i+1]);
  161. Inc(D,$5A827999);
  162. A := (A shl 30) + (A shr 2);
  163. Inc(C,(D shl 5) + (D shr 27) + (B xor (E and (A xor B))) + W[i+2]);
  164. Inc(C,$5A827999);
  165. E := (E shl 30) + (E shr 2);
  166. Inc(B,(C shl 5) + (C shr 27) + (A xor (D and (E xor A))) + W[i+3]);
  167. Inc(B,$5A827999);
  168. D := (D shl 30) + (D shr 2);
  169. Inc(A,(B shl 5) + (B shr 27) + (E xor (C and (D xor E))) + W[i+4]);
  170. Inc(A,$5A827999);
  171. C := (C shl 30) + (C shr 2);
  172. Inc(i,5);
  173. until i = 20;
  174. { The following three loops will only use the first 16 elements of the W
  175. array in a circular, recursive pattern. The following assignments are a
  176. trade-off to avoid having to split up the first loop. }
  177. W[0] := W[16];
  178. W[1] := W[17];
  179. W[2] := W[18];
  180. W[3] := W[19];
  181. { In the following three loops the recursive W array expansion is performed
  182. "just in time" following a circular pattern. Using circular indicies (e.g.
  183. (i+2) and $F) is not free, but the cost of declaring a large W array would
  184. be higher on dotNET. Before attempting to optimize this code, please note
  185. that the following language features are also costly:
  186. * Assignments and moves/copies, in particular on dotNET
  187. * Constant lookup tables, in particular on dotNET
  188. * Sub functions, in particular on x86
  189. * if..then and case..of. }
  190. i := 20;
  191. repeat
  192. T := W[(i+13) and $F] xor W[(i+8) and $F];
  193. T := T xor W[(i+2) and $F] xor W[i and $F];
  194. T := (T shl 1) or (T shr 31);
  195. W[i and $F] := T;
  196. Inc(E,(A shl 5) + (A shr 27) + (B xor C xor D) + T + $6ED9EBA1);
  197. B := (B shl 30) + (B shr 2);
  198. T := W[(i+14) and $F] xor W[(i+9) and $F];
  199. T := T xor W[(i+3) and $F] xor W[(i+1) and $F];
  200. T := (T shl 1) or (T shr 31);
  201. W[(i+1) and $F] := T;
  202. Inc(D,(E shl 5) + (E shr 27) + (A xor B xor C) + T + $6ED9EBA1);
  203. A := (A shl 30) + (A shr 2);
  204. T := W[(i+15) and $F] xor W[(i+10) and $F];
  205. T := T xor W[(i+4) and $F] xor W[(i+2) and $F];
  206. T := (T shl 1) or (T shr 31);
  207. W[(i+2) and $F] := T;
  208. Inc(C,(D shl 5) + (D shr 27) + (E xor A xor B) + T + $6ED9EBA1);
  209. E := (E shl 30) + (E shr 2);
  210. T := W[i and $F] xor W[(i+11) and $F];
  211. T := T xor W[(i+5) and $F] xor W[(i+3) and $F];
  212. T := (T shl 1) or (T shr 31);
  213. W[(i+3) and $F] := T;
  214. Inc(B,(C shl 5) + (C shr 27) + (D xor E xor A) + T + $6ED9EBA1);
  215. D := (D shl 30) + (D shr 2);
  216. T := W[(i+1) and $F] xor W[(i+12) and $F];
  217. T := T xor W[(i+6) and $F] xor W[(i+4) and $F];
  218. T := (T shl 1) or (T shr 31);
  219. W[(i+4) and $F] := T;
  220. Inc(A,(B shl 5) + (B shr 27) + (C xor D xor E) + T + $6ED9EBA1);
  221. C := (C shl 30) + (C shr 2);
  222. Inc(i,5);
  223. until i = 40;
  224. { Note that the constant $70E44324 = $100000000 - $8F1BBCDC has been selected
  225. to slightly reduce the probability that the CPU flag C (Carry) is set. This
  226. trick is taken from the StreamSec(R) StrSecII(TM) implementation of SHA-1.
  227. It entails a marginal but measurable performance gain on some CPUs. }
  228. i := 40;
  229. repeat
  230. T := W[(i+13) and $F] xor W[(i+8) and $F];
  231. T := T xor W[(i+2) and $F] xor W[i and $F];
  232. T := (T shl 1) or (T shr 31);
  233. W[i and $F] := T;
  234. Inc(E,(A shl 5) + (A shr 27) + ((B and C) or (D and (B or C))) + T);
  235. Dec(E,$70E44324);
  236. B := (B shl 30) + (B shr 2);
  237. T := W[(i+14) and $F] xor W[(i+9) and $F];
  238. T := T xor W[(i+3) and $F] xor W[(i+1) and $F];
  239. T := (T shl 1) or (T shr 31);
  240. W[(i+1) and $F] := T;
  241. Inc(D,(E shl 5) + (E shr 27) + ((A and B) or (C and (A or B))) + T);
  242. Dec(D,$70E44324);
  243. A := (A shl 30) + (A shr 2);
  244. T := W[(i+15) and $F] xor W[(i+10) and $F];
  245. T := T xor W[(i+4) and $F] xor W[(i+2) and $F];
  246. T := (T shl 1) or (T shr 31);
  247. W[(i+2) and $F] := T;
  248. Inc(C,(D shl 5) + (D shr 27) + ((E and A) or (B and (E or A))) + T);
  249. Dec(C,$70E44324);
  250. E := (E shl 30) + (E shr 2);
  251. T := W[i and $F] xor W[(i+11) and $F];
  252. T := T xor W[(i+5) and $F] xor W[(i+3) and $F];
  253. T := (T shl 1) or (T shr 31);
  254. W[(i+3) and $F] := T;
  255. Inc(B,(C shl 5) + (C shr 27) + ((D and E) or (A and (D or E))) + T);
  256. Dec(B,$70E44324);
  257. D := (D shl 30) + (D shr 2);
  258. T := W[(i+1) and $F] xor W[(i+12) and $F];
  259. T := T xor W[(i+6) and $F] xor W[(i+4) and $F];
  260. T := (T shl 1) or (T shr 31);
  261. W[(i+4) and $F] := T;
  262. Inc(A,(B shl 5) + (B shr 27) + ((C and D) or (E and (C or D))) + T);
  263. Dec(A,$70E44324);
  264. C := (C shl 30) + (C shr 2);
  265. Inc(i,5);
  266. until i = 60;
  267. { Note that the constant $359D3E2A = $100000000 - $CA62C1D6 has been selected
  268. to slightly reduce the probability that the CPU flag C (Carry) is set. This
  269. trick is taken from the StreamSec(R) StrSecII(TM) implementation of SHA-1.
  270. It entails a marginal but measurable performance gain on some CPUs. }
  271. repeat
  272. T := W[(i+13) and $F] xor W[(i+8) and $F];
  273. T := T xor W[(i+2) and $F] xor W[i and $F];
  274. T := (T shl 1) or (T shr 31);
  275. W[i and $F] := T;
  276. Inc(E,(A shl 5) + (A shr 27) + (B xor C xor D) + T - $359D3E2A);
  277. B := (B shl 30) + (B shr 2);
  278. T := W[(i+14) and $F] xor W[(i+9) and $F];
  279. T := T xor W[(i+3) and $F] xor W[(i+1) and $F];
  280. T := (T shl 1) or (T shr 31);
  281. W[(i+1) and $F] := T;
  282. Inc(D,(E shl 5) + (E shr 27) + (A xor B xor C) + T - $359D3E2A);
  283. A := (A shl 30) + (A shr 2);
  284. T := W[(i+15) and $F] xor W[(i+10) and $F];
  285. T := T xor W[(i+4) and $F] xor W[(i+2) and $F];
  286. T := (T shl 1) or (T shr 31);
  287. W[(i+2) and $F] := T;
  288. Inc(C,(D shl 5) + (D shr 27) + (E xor A xor B) + T - $359D3E2A);
  289. E := (E shl 30) + (E shr 2);
  290. T := W[i and $F] xor W[(i+11) and $F];
  291. T := T xor W[(i+5) and $F] xor W[(i+3) and $F];
  292. T := (T shl 1) or (T shr 31);
  293. W[(i+3) and $F] := T;
  294. Inc(B,(C shl 5) + (C shr 27) + (D xor E xor A) + T - $359D3E2A);
  295. D := (D shl 30) + (D shr 2);
  296. T := W[(i+1) and $F] xor W[(i+12) and $F];
  297. T := T xor W[(i+6) and $F] xor W[(i+4) and $F];
  298. T := (T shl 1) or (T shr 31);
  299. W[(i+4) and $F] := T;
  300. Inc(A,(B shl 5) + (B shr 27) + (C xor D xor E) + T - $359D3E2A);
  301. C := (C shl 30) + (C shr 2);
  302. Inc(i,5);
  303. until i = 80;
  304. FCheckSum[0]:= FCheckSum[0] + A;
  305. FCheckSum[1]:= FCheckSum[1] + B;
  306. FCheckSum[2]:= FCheckSum[2] + C;
  307. FCheckSum[3]:= FCheckSum[3] + D;
  308. FCheckSum[4]:= FCheckSum[4] + E;
  309. end;
  310. function TIdHashSHA1.NativeGetHashBytes(AStream: TStream; ASize: Int64): TIdBytes;
  311. var
  312. LSize: Integer;
  313. LLenHi: UInt32;
  314. LLenLo: UInt32;
  315. I: Integer;
  316. begin
  317. Result := nil;
  318. FCheckSum[0] := $67452301;
  319. FCheckSum[1] := $EFCDAB89;
  320. FCheckSum[2] := $98BADCFE;
  321. FCheckSum[3] := $10325476;
  322. FCheckSum[4] := $C3D2E1F0;
  323. LLenHi := 0;
  324. LLenLo := 0;
  325. // Code the entire file in complete 64-byte chunks.
  326. while ASize >= 64 do begin
  327. LSize := ReadTIdBytesFromStream(AStream, FCBuffer, 64);
  328. // TODO: handle stream read error
  329. Inc(LLenLo, LSize * 8);
  330. if LLenLo < UInt32(LSize * 8) then begin
  331. Inc(LLenHi);
  332. end;
  333. Coder;
  334. Dec(ASize, LSize);
  335. end;
  336. // Read the last set of bytes.
  337. LSize := ReadTIdBytesFromStream(AStream, FCBuffer, ASize);
  338. // TODO: handle stream read error
  339. Inc(LLenLo, LSize * 8);
  340. if LLenLo < UInt32(LSize * 8) then begin
  341. Inc(LLenHi);
  342. end;
  343. FCBuffer[LSize] := $80;
  344. if LSize >= 56 then begin
  345. for I := (LSize + 1) to 63 do begin
  346. FCBuffer[i] := 0;
  347. end;
  348. Coder;
  349. LSize := -1;
  350. end;
  351. for I := (LSize + 1) to 55 do begin
  352. FCBuffer[i] := 0;
  353. end;
  354. FCBuffer[56] := (LLenHi shr 24);
  355. FCBuffer[57] := (LLenHi shr 16) and $FF;
  356. FCBuffer[58] := (LLenHi shr 8) and $FF;
  357. FCBuffer[59] := (LLenHi and $FF);
  358. FCBuffer[60] := (LLenLo shr 24);
  359. FCBuffer[61] := (LLenLo shr 16) and $FF;
  360. FCBuffer[62] := (LLenLo shr 8) and $FF;
  361. FCBuffer[63] := (LLenLo and $FF);
  362. Coder;
  363. FCheckSum[0] := SwapUInt32(FCheckSum[0]);
  364. FCheckSum[1] := SwapUInt32(FCheckSum[1]);
  365. FCheckSum[2] := SwapUInt32(FCheckSum[2]);
  366. FCheckSum[3] := SwapUInt32(FCheckSum[3]);
  367. FCheckSum[4] := SwapUInt32(FCheckSum[4]);
  368. SetLength(Result, SizeOf(UInt32)*5);
  369. for I := 0 to 4 do begin
  370. CopyTIdUInt32(FCheckSum[I], Result, SizeOf(UInt32)*I);
  371. end;
  372. end;
  373. function TIdHashSHA1.HashToHex(const AHash: TIdBytes): String;
  374. begin
  375. Result := UInt32HashToHex(AHash, 5);
  376. end;
  377. class function TIdHashSHA1.IsAvailable : Boolean;
  378. begin
  379. Result := True;
  380. end;
  381. { TIdHashSHA224 }
  382. function TIdHashSHA224.InitHash: TIdHashIntCtx;
  383. begin
  384. Result := GetSHA224HashInst;
  385. end;
  386. class function TIdHashSHA224.IsAvailable: Boolean;
  387. begin
  388. Result := IsHashingIntfAvail and IsSHA224HashIntfAvail;
  389. end;
  390. { TIdHashSHA256 }
  391. function TIdHashSHA256.InitHash: TIdHashIntCtx;
  392. begin
  393. Result := GetSHA256HashInst;
  394. end;
  395. class function TIdHashSHA256.IsAvailable : Boolean;
  396. begin
  397. Result := IsHashingIntfAvail and IsSHA256HashIntfAvail;
  398. end;
  399. { TIdHashSHA384 }
  400. function TIdHashSHA384.InitHash: TIdHashIntCtx;
  401. begin
  402. Result := GetSHA384HashInst;
  403. end;
  404. class function TIdHashSHA384.IsAvailable: Boolean;
  405. begin
  406. Result := IsHashingIntfAvail and IsSHA384HashIntfAvail;
  407. end;
  408. { TIdHashSHA512 }
  409. function TIdHashSHA512.InitHash: TIdHashIntCtx;
  410. begin
  411. Result := GetSHA512HashInst;
  412. end;
  413. class function TIdHashSHA512.IsAvailable: Boolean;
  414. begin
  415. Result := IsHashingIntfAvail and IsSHA512HashIntfAvail;
  416. end;
  417. end.