1
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* Distributed under the terms of the BSD-modified licence:
|
|
6 |
*
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8 |
* of this software and associated documentation files (the "Software"), to deal
|
|
9 |
* with the Software without restriction, including without limitation the
|
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
12 |
* furnished to do so, subject to the following conditions:
|
|
13 |
*
|
|
14 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
15 |
* this list of conditions and the following disclaimer.
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
17 |
* this list of conditions and the following disclaimer in the documentation
|
|
18 |
* and/or other materials provided with the distribution.
|
|
19 |
* 3. The name of the author may not be used to endorse or promote products
|
|
20 |
* derived from this software without specific prior written permission.
|
|
21 |
*
|
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32 |
*)
|
|
33 |
|
|
34 |
unit uSHA;
|
|
35 |
interface
|
|
36 |
|
|
37 |
type TSHA1Context = packed record
|
|
38 |
H: array[0..4] of LongWord;
|
|
39 |
Length, CurrLength: Int64;
|
|
40 |
Buf: array[0..63] of byte;
|
|
41 |
end;
|
|
42 |
TSHA1Digest = record
|
|
43 |
case byte of
|
|
44 |
0: (LongWords: array[0.. 4] of LongWord);
|
|
45 |
1: ( Words: array[0.. 9] of Word);
|
|
46 |
2: ( Bytes: array[0..19] of Byte)
|
|
47 |
end;
|
|
48 |
|
|
49 |
procedure SHA1Init(var Context: TSHA1Context);
|
|
50 |
procedure SHA1Update(var Context: TSHA1Context; Buf: Pointer; Length: LongWord);
|
|
51 |
function SHA1Final(Context: TSHA1Context): TSHA1Digest;
|
|
52 |
|
|
53 |
implementation
|
|
54 |
|
|
55 |
function _bswap(X: LongWord): LongWord; assembler;
|
|
56 |
asm
|
|
57 |
bswap eax
|
|
58 |
end;
|
|
59 |
|
|
60 |
function rol(x: LongWord; y: Byte): LongWord; assembler;
|
|
61 |
asm
|
|
62 |
mov cl,dl
|
|
63 |
rol eax,cl
|
|
64 |
end;
|
|
65 |
|
|
66 |
function Ft(t, b, c, d: LongWord): LongWord;
|
|
67 |
begin
|
|
68 |
case t of
|
|
69 |
0..19: Result := (b and c) or ((not b) and d);
|
|
70 |
20..39: Result := b xor c xor d;
|
|
71 |
40..59: Result := (b and c) or (b and d) or (c and d);
|
|
72 |
else Result := b xor c xor d;
|
|
73 |
end;
|
|
74 |
end;
|
|
75 |
|
|
76 |
function Kt(t: Byte): LongWord;
|
|
77 |
begin
|
|
78 |
case t of
|
|
79 |
0..19: Result := $5A827999;
|
|
80 |
20..39: Result := $6ED9EBA1;
|
|
81 |
40..59: Result := $8F1BBCDC;
|
|
82 |
else
|
|
83 |
Result := $CA62C1D6
|
|
84 |
end;
|
|
85 |
end;
|
|
86 |
|
|
87 |
|
|
88 |
procedure SHA1Hash(var Context: TSHA1Context);
|
|
89 |
var S: array[0..4 ] of LongWord;
|
|
90 |
W: array[0..79] of LongWord;
|
|
91 |
i, t: LongWord;
|
|
92 |
begin
|
|
93 |
move(Context.H, S, sizeof(S));
|
|
94 |
for i:= 0 to 15 do
|
|
95 |
W[i]:= _bswap(PLongWord(LongWord(@Context.Buf)+i*4)^);
|
|
96 |
for i := 16 to 79 do
|
|
97 |
W[i] := rol(W[i - 3] xor W[i - 8] xor W[i - 14] xor W[i - 16], 1);
|
|
98 |
for i := 0 to 79 do
|
|
99 |
begin
|
|
100 |
t:= rol(S[0], 5) + Ft(i, S[1], S[2], S[3]) + S[4] + W[i] + Kt(i);
|
|
101 |
S[4]:= S[3];
|
|
102 |
S[3]:= S[2];
|
|
103 |
S[2]:= rol(S[1], 30);
|
|
104 |
S[1]:= S[0];
|
|
105 |
S[0]:= t
|
|
106 |
end;
|
|
107 |
for i := 0 to 4 do
|
|
108 |
Context.H[i]:= Context.H[i] + S[i]
|
|
109 |
end;
|
|
110 |
|
|
111 |
procedure SHA1Init(var Context: TSHA1Context);
|
|
112 |
begin
|
|
113 |
with Context do
|
|
114 |
begin
|
|
115 |
Length := 0;
|
|
116 |
CurrLength:= 0;
|
|
117 |
H[0]:= $67452301;
|
|
118 |
H[1]:= $EFCDAB89;
|
|
119 |
H[2]:= $98BADCFE;
|
|
120 |
H[3]:= $10325476;
|
|
121 |
H[4]:= $C3D2E1F0
|
|
122 |
end
|
|
123 |
end;
|
|
124 |
|
|
125 |
procedure SHA1Update(var Context: TSHA1Context; Buf: Pointer; Length: LongWord);
|
|
126 |
var i: integer;
|
|
127 |
begin
|
|
128 |
for i:= 1 to Length do
|
|
129 |
begin
|
|
130 |
Context.Buf[Context.CurrLength]:= PByte(Buf)^;
|
|
131 |
inc(Context.CurrLength);
|
|
132 |
inc(LongWord(Buf));
|
|
133 |
if Context.CurrLength=64 then
|
|
134 |
begin
|
|
135 |
SHA1Hash(Context);
|
|
136 |
inc(Context.Length, 512);
|
|
137 |
Context.CurrLength:=0
|
|
138 |
end
|
|
139 |
end
|
|
140 |
end;
|
|
141 |
|
|
142 |
function SHA1Final(Context: TSHA1Context): TSHA1Digest;
|
|
143 |
var i: LongWord;
|
|
144 |
begin
|
|
145 |
Context.Length:= Context.Length + Context.CurrLength shl 3;
|
|
146 |
Context.Buf[Context.CurrLength]:= $80;
|
|
147 |
inc(Context.CurrLength);
|
|
148 |
if Context.CurrLength>56 then
|
|
149 |
begin
|
|
150 |
FillChar(Context.Buf[Context.CurrLength],64-Context.CurrLength,0);
|
|
151 |
Context.CurrLength:= 64;
|
|
152 |
SHA1Hash(Context);
|
|
153 |
Context.CurrLength:=0
|
|
154 |
end;
|
|
155 |
FillChar(Context.Buf[Context.CurrLength],56-Context.CurrLength,0);
|
|
156 |
for i:= 56 to 63 do
|
|
157 |
Context.Buf[i] := (Context.Length shr ((63 - i) * 8)) and $FF;
|
|
158 |
SHA1Hash(Context);
|
|
159 |
move(Context.H, Result, sizeof(TSHA1Digest));
|
|
160 |
FillChar(Context, sizeof(Context), 0)
|
|
161 |
end;
|
|
162 |
|
|
163 |
end.
|