| 1 |
unit SHA1; |
| 2 |
|
| 3 |
{SHA1 - 160 bit Secure Hash Function} |
| 4 |
|
| 5 |
|
| 6 |
interface |
| 7 |
|
| 8 |
(************************************************************************* |
| 9 |
|
| 10 |
DESCRIPTION : SHA1 - 160 bit Secure Hash Function |
| 11 |
|
| 12 |
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12/D17-D18/D25S, FPC, VP |
| 13 |
|
| 14 |
EXTERNAL DATA : --- |
| 15 |
|
| 16 |
MEMORY USAGE : --- |
| 17 |
|
| 18 |
DISPLAY MODE : --- |
| 19 |
|
| 20 |
REFERENCES : - Latest specification of Secure Hash Standard: |
| 21 |
http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf |
| 22 |
- Test vectors and intermediate values: |
| 23 |
http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf |
| 24 |
|
| 25 |
|
| 26 |
Version Date Author Modification |
| 27 |
------- -------- ------- ------------------------------------------ |
| 28 |
1.00 03.01.02 W.Ehrhardt BP7 implementation |
| 29 |
1.01 14.03.02 we D1-D6, FPC, VP |
| 30 |
1.02 14.03.02 we TP6 |
| 31 |
1.03 14.03.02 we TP6/7 386-Code |
| 32 |
1.04 14.03.02 we TP5.5 |
| 33 |
1.10 15.03.02 we self test with 2 strings |
| 34 |
1.11 02.01.03 we const SFA with @ for FPC 1.0.6 |
| 35 |
1.20 23.07.03 we With SHA1File, SHA1Full |
| 36 |
1.21 26.07.03 we With SHA1Full in self test |
| 37 |
2.00 26.07.03 we common vers., longint for word32, D4+ - warnings |
| 38 |
2.01 03.08.03 we type TSHA1Block for HMAC |
| 39 |
2.02 23.08.03 we SHA1Compress in interface for prng |
| 40 |
2.10 29.08.03 we XL versions for Win32 |
| 41 |
2.20 27.09.03 we FPC/go32v2 |
| 42 |
2.30 05.10.03 we STD.INC, TP5.0 |
| 43 |
2.40 10.10.03 we common version, english comments |
| 44 |
2.45 11.10.03 we Speedup: partial unroll, no function calls |
| 45 |
2.50 16.11.03 we Speedup in update, don't clear W in compress |
| 46 |
2.51 17.11.03 we BIT16: partial unroll, BIT32: inline rot |
| 47 |
2.52 17.11.03 we ExpandMessageBlocks |
| 48 |
2.53 18.11.03 we LRot32, RB mit inline() |
| 49 |
2.54 20.11.03 we Full range UpdateLen |
| 50 |
2.55 30.11.03 we BIT16: {$F-} |
| 51 |
2.56 30.11.03 we BIT16: LRot_5, LRot_30 |
| 52 |
3.00 01.12.03 we Common version 3.0 |
| 53 |
3.01 22.12.03 we BIT16: Two INCs |
| 54 |
3.02 22.12.03 we BASM16: asm Lrot30 |
| 55 |
3.03 22.12.03 we TP5/5.5: LRot, RA inline |
| 56 |
3.04 22,12.03 we Changed UpdateLen: Definition and TP5/5.5 inline |
| 57 |
3.05 05.03.04 we Update fips180-2 URL |
| 58 |
3.06 26.02.05 we With {$ifdef StrictLong} |
| 59 |
3.07 05.05.05 we Use longint() in SH1Init to avoid D9 errors if $R+ |
| 60 |
3.08 17.12.05 we Force $I- in SHA1File |
| 61 |
3.09 08.01.06 we SHA1Compress removed from interface |
| 62 |
3.10 15.01.06 we uses Hash unit and THashDesc |
| 63 |
3.11 18.01.06 we Descriptor fields HAlgNum, HSig |
| 64 |
3.12 22.01.06 we Removed HSelfTest from descriptor |
| 65 |
3.13 11.02.06 we Descriptor as typed const |
| 66 |
3.14 26.03.06 we Round constants K1..K4, code reordering |
| 67 |
3.15 07.08.06 we $ifdef BIT32: (const fname: shortstring...) |
| 68 |
3.16 22.02.07 we values for OID vector |
| 69 |
3.17 30.06.07 we Use conditional define FPC_ProcVar |
| 70 |
3.18 04.10.07 we FPC: {$asmmode intel} |
| 71 |
3.19 02.05.08 we Bit-API: SHA1FinalBits/Ex |
| 72 |
3.20 05.05.08 we THashDesc constant with HFinalBit field |
| 73 |
3.21 12.11.08 we uses BTypes, Ptr2Inc and/or Str255/Str127 |
| 74 |
3.22 12.03.10 we Fix VP feature in ExpandMessageBlocks |
| 75 |
3.23 11.03.12 we Updated references |
| 76 |
3.24 26.12.12 we D17 and PurePascal |
| 77 |
3.25 16.08.15 we Removed $ifdef DLL / stdcall |
| 78 |
3.26 15.05.17 we adjust OID to new MaxOIDLen |
| 79 |
**************************************************************************) |
| 80 |
|
| 81 |
|
| 82 |
(*------------------------------------------------------------------------- |
| 83 |
(C) Copyright 2002-2017 Wolfgang Ehrhardt |
| 84 |
|
| 85 |
This software is provided 'as-is', without any express or implied warranty. |
| 86 |
In no event will the authors be held liable for any damages arising from |
| 87 |
the use of this software. |
| 88 |
|
| 89 |
Permission is granted to anyone to use this software for any purpose, |
| 90 |
including commercial applications, and to alter it and redistribute it |
| 91 |
freely, subject to the following restrictions: |
| 92 |
|
| 93 |
1. The origin of this software must not be misrepresented; you must not |
| 94 |
claim that you wrote the original software. If you use this software in |
| 95 |
a product, an acknowledgment in the product documentation would be |
| 96 |
appreciated but is not required. |
| 97 |
|
| 98 |
2. Altered source versions must be plainly marked as such, and must not be |
| 99 |
misrepresented as being the original software. |
| 100 |
|
| 101 |
3. This notice may not be removed or altered from any source distribution. |
| 102 |
----------------------------------------------------------------------------*) |
| 103 |
|
| 104 |
{NOTE: FIPS Ch and May functions can be optimized. Wei Dai (Crypto++ 3.1) |
| 105 |
credits Rich Schroeppel ([email protected]), V 5.1 does not!?} |
| 106 |
|
| 107 |
|
| 108 |
{$i STD.INC} |
| 109 |
|
| 110 |
{$ifndef CPUI386} |
| 111 |
{$ifndef PurePascal} |
| 112 |
{$define PurePascal} |
| 113 |
{$endif} |
| 114 |
{$endif} |
| 115 |
|
| 116 |
uses |
| 117 |
BTypes,Hash; |
| 118 |
|
| 119 |
|
| 120 |
procedure SHA1Init(var Context: THashContext); |
| 121 |
{-initialize context} |
| 122 |
|
| 123 |
procedure SHA1Update(var Context: THashContext; Msg: pointer; Len: word); |
| 124 |
{-update context with Msg data} |
| 125 |
|
| 126 |
procedure SHA1UpdateXL(var Context: THashContext; Msg: pointer; Len: longint); |
| 127 |
{-update context with Msg data} |
| 128 |
|
| 129 |
procedure SHA1Final(var Context: THashContext; var Digest: TSHA1Digest); |
| 130 |
{-finalize SHA1 calculation, clear context} |
| 131 |
|
| 132 |
procedure SHA1FinalEx(var Context: THashContext; var Digest: THashDigest); |
| 133 |
{-finalize SHA1 calculation, clear context} |
| 134 |
|
| 135 |
procedure SHA1FinalBitsEx(var Context: THashContext; var Digest: THashDigest; BData: byte; bitlen: integer); |
| 136 |
{-finalize SHA1 calculation with bitlen bits from BData (big-endian), clear context} |
| 137 |
|
| 138 |
procedure SHA1FinalBits(var Context: THashContext; var Digest: TSHA1Digest; BData: byte; bitlen: integer); |
| 139 |
{-finalize SHA1 calculation with bitlen bits from BData (big-endian), clear context} |
| 140 |
|
| 141 |
function SHA1SelfTest: boolean; |
| 142 |
{-self test SHA1: compare with known value} |
| 143 |
|
| 144 |
procedure SHA1Full(var Digest: TSHA1Digest; Msg: pointer; Len: word); |
| 145 |
{-SHA1 of Msg with init/update/final} |
| 146 |
|
| 147 |
procedure SHA1FullXL(var Digest: TSHA1Digest; Msg: pointer; Len: longint); |
| 148 |
{-SHA1 of Msg with init/update/final} |
| 149 |
|
| 150 |
procedure SHA1File({$ifdef CONST} const {$endif} fname: Str255; |
| 151 |
var Digest: TSHA1Digest; var buf; bsize: word; var Err: word); |
| 152 |
{-SHA1 of file, buf: buffer with at least bsize bytes} |
| 153 |
|
| 154 |
|
| 155 |
implementation |
| 156 |
|
| 157 |
{$ifdef BIT16} |
| 158 |
{$F-} |
| 159 |
{$endif} |
| 160 |
|
| 161 |
const |
| 162 |
SHA1_BlockLen = 64; |
| 163 |
|
| 164 |
const {round constants} |
| 165 |
K1 = longint($5A827999); {round 00..19} |
| 166 |
K2 = longint($6ED9EBA1); {round 20..39} |
| 167 |
K3 = longint($8F1BBCDC); {round 40..59} |
| 168 |
K4 = longint($CA62C1D6); {round 60..79} |
| 169 |
|
| 170 |
|
| 171 |
{Internal types} |
| 172 |
type |
| 173 |
TWorkBuf = array[0..79] of longint; |
| 174 |
|
| 175 |
{1.3.14.3.2.26} |
| 176 |
{iso(1) identified-organization(3) oiw(14) secsig(3) algorithms(2) hashAlgorithmIdentifier(26)} |
| 177 |
const |
| 178 |
SHA1_OID : TOID_Vec = (1,3,14,3,2,26,-1,-1,-1,-1,-1); {Len=6} |
| 179 |
|
| 180 |
{$ifndef VER5X} |
| 181 |
const |
| 182 |
SHA1_Desc: THashDesc = ( |
| 183 |
HSig : C_HashSig; |
| 184 |
HDSize : sizeof(THashDesc); |
| 185 |
HDVersion : C_HashVers; |
| 186 |
HBlockLen : SHA1_BlockLen; |
| 187 |
HDigestlen: sizeof(TSHA1Digest); |
| 188 |
{$ifdef FPC_ProcVar} |
| 189 |
HInit : @SHA1Init; |
| 190 |
HFinal : @SHA1FinalEx; |
| 191 |
HUpdateXL : @SHA1UpdateXL; |
| 192 |
{$else} |
| 193 |
HInit : SHA1Init; |
| 194 |
HFinal : SHA1FinalEx; |
| 195 |
HUpdateXL : SHA1UpdateXL; |
| 196 |
{$endif} |
| 197 |
HAlgNum : longint(_SHA1); |
| 198 |
HName : 'SHA1'; |
| 199 |
HPtrOID : @SHA1_OID; |
| 200 |
HLenOID : 6; |
| 201 |
HFill : 0; |
| 202 |
{$ifdef FPC_ProcVar} |
| 203 |
HFinalBit : @SHA1FinalBitsEx; |
| 204 |
{$else} |
| 205 |
HFinalBit : SHA1FinalBitsEx; |
| 206 |
{$endif} |
| 207 |
HReserved : (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) |
| 208 |
); |
| 209 |
{$else} |
| 210 |
var |
| 211 |
SHA1_Desc: THashDesc; |
| 212 |
{$endif} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
{$ifndef BIT16} |
| 217 |
|
| 218 |
{$ifdef PurePascal} |
| 219 |
{---------------------------------------------------------------------------} |
| 220 |
procedure UpdateLen(var whi, wlo: longint; BLen: longint); |
| 221 |
{-Add BLen to 64 bit value (wlo, whi)} |
| 222 |
var |
| 223 |
tmp: int64; |
| 224 |
begin |
| 225 |
tmp := int64(cardinal(wlo))+Blen; |
| 226 |
wlo := longint(tmp and $FFFFFFFF); |
| 227 |
inc(whi,longint(tmp shr 32)); |
| 228 |
end; |
| 229 |
|
| 230 |
{---------------------------------------------------------------------------} |
| 231 |
function RB(A: longint): longint; |
| 232 |
{-reverse byte order in longint} |
| 233 |
begin |
| 234 |
RB := ((A and $FF) shl 24) or ((A and $FF00) shl 8) or ((A and $FF0000) shr 8) or ((A and longint($FF000000)) shr 24); |
| 235 |
end; |
| 236 |
|
| 237 |
{---------------------------------------------------------------------------} |
| 238 |
procedure ExpandMessageBlocks(var W: TWorkBuf; var Buf: THashBuffer); |
| 239 |
{-Calculate "expanded message blocks"} |
| 240 |
var |
| 241 |
i,T: longint; |
| 242 |
begin |
| 243 |
{Part 1: Transfer buffer with little -> big endian conversion} |
| 244 |
for i:= 0 to 15 do W[i]:= RB(THashBuf32(Buf)[i]); |
| 245 |
{Part 2: Calculate remaining "expanded message blocks"} |
| 246 |
for i:= 16 to 79 do begin |
| 247 |
T := W[i-3] xor W[i-8] xor W[i-14] xor W[i-16]; |
| 248 |
W[i] := (T shl 1) or (T shr 31); |
| 249 |
end; |
| 250 |
end; |
| 251 |
|
| 252 |
{$else} |
| 253 |
{---------------------------------------------------------------------------} |
| 254 |
procedure UpdateLen(var whi, wlo: longint; BLen: longint); |
| 255 |
{-Add BLen to 64 bit value (wlo, whi)} |
| 256 |
begin |
| 257 |
asm |
| 258 |
mov edx, [wlo] |
| 259 |
mov ecx, [whi] |
| 260 |
mov eax, [Blen] |
| 261 |
add [edx], eax |
| 262 |
adc dword ptr [ecx], 0 |
| 263 |
end; |
| 264 |
end; |
| 265 |
|
| 266 |
{---------------------------------------------------------------------------} |
| 267 |
function RB(A: longint): longint; assembler; |
| 268 |
{-reverse byte order in longint} |
| 269 |
asm |
| 270 |
{$ifdef LoadArgs} |
| 271 |
mov eax,[A] |
| 272 |
{$endif} |
| 273 |
xchg al,ah |
| 274 |
rol eax,16 |
| 275 |
xchg al,ah |
| 276 |
end; |
| 277 |
|
| 278 |
{---------------------------------------------------------------------------} |
| 279 |
procedure ExpandMessageBlocks(var W: TWorkBuf; var Buf: THashBuffer); assembler; |
| 280 |
{-Calculate "expanded message blocks"} |
| 281 |
asm |
| 282 |
{$ifdef LoadArgs} |
| 283 |
mov edx,Buf |
| 284 |
mov ecx,W {load W before push ebx to avoid VP crash} |
| 285 |
push ebx {if compiling with no ASM stack frames} |
| 286 |
mov ebx,ecx |
| 287 |
{$else} |
| 288 |
push ebx |
| 289 |
mov ebx,eax |
| 290 |
{$endif} |
| 291 |
{part1: W[i]:= RB(TW32Buf(Buf)[i])} |
| 292 |
mov ecx,16 |
| 293 |
@@1: mov eax,[edx] |
| 294 |
xchg al,ah |
| 295 |
rol eax,16 |
| 296 |
xchg al,ah |
| 297 |
mov [ebx],eax |
| 298 |
add ebx,4 |
| 299 |
add edx,4 |
| 300 |
dec ecx |
| 301 |
jnz @@1 |
| 302 |
{part2: W[i]:= LRot_1(W[i-3] xor W[i-8] xor W[i-14] xor W[i-16]);} |
| 303 |
mov ecx,64 |
| 304 |
@@2: mov eax,[ebx- 3*4] |
| 305 |
xor eax,[ebx- 8*4] |
| 306 |
xor eax,[ebx-14*4] |
| 307 |
xor eax,[ebx-16*4] |
| 308 |
rol eax,1 |
| 309 |
mov [ebx],eax |
| 310 |
add ebx,4 |
| 311 |
dec ecx |
| 312 |
jnz @@2 |
| 313 |
pop ebx |
| 314 |
end; |
| 315 |
{$endif} |
| 316 |
|
| 317 |
|
| 318 |
{---------------------------------------------------------------------------} |
| 319 |
procedure SHA1Compress(var Data: THashContext); |
| 320 |
{-Actual hashing function} |
| 321 |
var |
| 322 |
i: integer; |
| 323 |
A, B, C, D, E: longint; |
| 324 |
W: TWorkBuf; |
| 325 |
begin |
| 326 |
|
| 327 |
ExpandMessageBlocks(W, Data.Buffer); |
| 328 |
|
| 329 |
A := Data.Hash[0]; |
| 330 |
B := Data.Hash[1]; |
| 331 |
C := Data.Hash[2]; |
| 332 |
D := Data.Hash[3]; |
| 333 |
E := Data.Hash[4]; |
| 334 |
|
| 335 |
{SHA1 compression function} |
| 336 |
{Partial unroll for more speed, full unroll is only slightly faster} |
| 337 |
{BIT32: rotateleft via inline} |
| 338 |
i := 0; |
| 339 |
while i<20 do begin |
| 340 |
inc(E, (A shl 5 or A shr 27) + (D xor (B and (C xor D))) + W[i ] + K1); B := B shr 2 or B shl 30; |
| 341 |
inc(D, (E shl 5 or E shr 27) + (C xor (A and (B xor C))) + W[i+1] + K1); A := A shr 2 or A shl 30; |
| 342 |
inc(C, (D shl 5 or D shr 27) + (B xor (E and (A xor B))) + W[i+2] + K1); E := E shr 2 or E shl 30; |
| 343 |
inc(B, (C shl 5 or C shr 27) + (A xor (D and (E xor A))) + W[i+3] + K1); D := D shr 2 or D shl 30; |
| 344 |
inc(A, (B shl 5 or B shr 27) + (E xor (C and (D xor E))) + W[i+4] + K1); C := C shr 2 or C shl 30; |
| 345 |
inc(i,5); |
| 346 |
end; |
| 347 |
while i<40 do begin |
| 348 |
inc(E, (A shl 5 or A shr 27) + (D xor B xor C) + W[i ] + K2); B := B shr 2 or B shl 30; |
| 349 |
inc(D, (E shl 5 or E shr 27) + (C xor A xor B) + W[i+1] + K2); A := A shr 2 or A shl 30; |
| 350 |
inc(C, (D shl 5 or D shr 27) + (B xor E xor A) + W[i+2] + K2); E := E shr 2 or E shl 30; |
| 351 |
inc(B, (C shl 5 or C shr 27) + (A xor D xor E) + W[i+3] + K2); D := D shr 2 or D shl 30; |
| 352 |
inc(A, (B shl 5 or B shr 27) + (E xor C xor D) + W[i+4] + K2); C := C shr 2 or C shl 30; |
| 353 |
inc(i,5); |
| 354 |
end; |
| 355 |
while i<60 do begin |
| 356 |
inc(E, (A shl 5 or A shr 27) + ((B and C) or (D and (B or C))) + W[i ] + K3); B := B shr 2 or B shl 30; |
| 357 |
inc(D, (E shl 5 or E shr 27) + ((A and B) or (C and (A or B))) + W[i+1] + K3); A := A shr 2 or A shl 30; |
| 358 |
inc(C, (D shl 5 or D shr 27) + ((E and A) or (B and (E or A))) + W[i+2] + K3); E := E shr 2 or E shl 30; |
| 359 |
inc(B, (C shl 5 or C shr 27) + ((D and E) or (A and (D or E))) + W[i+3] + K3); D := D shr 2 or D shl 30; |
| 360 |
inc(A, (B shl 5 or B shr 27) + ((C and D) or (E and (C or D))) + W[i+4] + K3); C := C shr 2 or C shl 30; |
| 361 |
inc(i,5); |
| 362 |
end; |
| 363 |
while i<80 do begin |
| 364 |
inc(E, (A shl 5 or A shr 27) + (D xor B xor C) + W[i ] + K4); B := B shr 2 or B shl 30; |
| 365 |
inc(D, (E shl 5 or E shr 27) + (C xor A xor B) + W[i+1] + K4); A := A shr 2 or A shl 30; |
| 366 |
inc(C, (D shl 5 or D shr 27) + (B xor E xor A) + W[i+2] + K4); E := E shr 2 or E shl 30; |
| 367 |
inc(B, (C shl 5 or C shr 27) + (A xor D xor E) + W[i+3] + K4); D := D shr 2 or D shl 30; |
| 368 |
inc(A, (B shl 5 or B shr 27) + (E xor C xor D) + W[i+4] + K4); C := C shr 2 or C shl 30; |
| 369 |
inc(i,5); |
| 370 |
end; |
| 371 |
|
| 372 |
{Calculate new working hash} |
| 373 |
inc(Data.Hash[0], A); |
| 374 |
inc(Data.Hash[1], B); |
| 375 |
inc(Data.Hash[2], C); |
| 376 |
inc(Data.Hash[3], D); |
| 377 |
inc(Data.Hash[4], E); |
| 378 |
end; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
{$else} |
| 383 |
|
| 384 |
|
| 385 |
{$ifdef BASM16} |
| 386 |
|
| 387 |
{TP6-7/Delphi1 for 386+} |
| 388 |
|
| 389 |
{---------------------------------------------------------------------------} |
| 390 |
procedure UpdateLen(var whi, wlo: longint; BLen: longint); assembler; |
| 391 |
{-Add BLen to 64 bit value (wlo, whi)} |
| 392 |
asm |
| 393 |
les di,[wlo] |
| 394 |
db $66; mov ax,word ptr [BLen] |
| 395 |
db $66; sub dx,dx |
| 396 |
db $66; add es:[di],ax |
| 397 |
les di,[whi] |
| 398 |
db $66; adc es:[di],dx |
| 399 |
end; |
| 400 |
|
| 401 |
|
| 402 |
{---------------------------------------------------------------------------} |
| 403 |
function LRot_5(x: longint): longint; |
| 404 |
{-Rotate left 5} |
| 405 |
inline( |
| 406 |
$66/$58/ {pop eax } |
| 407 |
$66/$C1/$C0/$05/ {rol eax,5 } |
| 408 |
$66/$8B/$D0/ {mov edx,eax} |
| 409 |
$66/$C1/$EA/$10); {shr edx,16 } |
| 410 |
|
| 411 |
|
| 412 |
{---------------------------------------------------------------------------} |
| 413 |
function RB(A: longint): longint; |
| 414 |
{-reverse byte order in longint} |
| 415 |
inline( |
| 416 |
$58/ {pop ax } |
| 417 |
$5A/ {pop dx } |
| 418 |
$86/$C6/ {xchg dh,al } |
| 419 |
$86/$E2); {xchg dl,ah } |
| 420 |
|
| 421 |
|
| 422 |
{---------------------------------------------------------------------------} |
| 423 |
procedure ExpandMessageBlocks(var W: TWorkBuf; var Buf: THashBuffer); assembler; |
| 424 |
{-Calculate "expanded message blocks"} |
| 425 |
asm |
| 426 |
push ds |
| 427 |
{part 1: W[i]:= RB(TW32Buf(Buf)[i])} |
| 428 |
les di,[Buf] |
| 429 |
lds si,[W] |
| 430 |
mov cx,16 |
| 431 |
@@1: db $66; mov ax,es:[di] |
| 432 |
xchg al,ah |
| 433 |
db $66; rol ax,16 |
| 434 |
xchg al,ah |
| 435 |
db $66; mov [si],ax |
| 436 |
add si,4 |
| 437 |
add di,4 |
| 438 |
dec cx |
| 439 |
jnz @@1 |
| 440 |
{part 2: W[i]:= LRot_1(W[i-3] xor W[i-8] xor W[i-14] xor W[i-16]);} |
| 441 |
mov cx,64 |
| 442 |
@@2: db $66; mov ax,[si- 3*4] |
| 443 |
db $66; xor ax,[si- 8*4] |
| 444 |
db $66; xor ax,[si-14*4] |
| 445 |
db $66; xor ax,[si-16*4] |
| 446 |
db $66; rol ax,1 |
| 447 |
db $66; mov [si],ax |
| 448 |
add si,4 |
| 449 |
dec cx |
| 450 |
jnz @@2 |
| 451 |
pop ds |
| 452 |
end; |
| 453 |
|
| 454 |
{---------------------------------------------------------------------------} |
| 455 |
procedure SHA1Compress(var Data: THashContext); |
| 456 |
{-Actual hashing function} |
| 457 |
var |
| 458 |
i: integer; |
| 459 |
A, B, C, D, E: longint; |
| 460 |
W: TWorkBuf; |
| 461 |
begin |
| 462 |
ExpandMessageBlocks(W, Data.Buffer); |
| 463 |
{Assign old working hash to variables A..E} |
| 464 |
A := Data.Hash[0]; |
| 465 |
B := Data.Hash[1]; |
| 466 |
C := Data.Hash[2]; |
| 467 |
D := Data.Hash[3]; |
| 468 |
E := Data.Hash[4]; |
| 469 |
|
| 470 |
{SHA1 compression function} |
| 471 |
{Partial unroll for more speed, full unroll only marginally faster} |
| 472 |
{Two INCs, LRot_30 via BASM} |
| 473 |
i := 0; |
| 474 |
while i<20 do begin |
| 475 |
inc(E,LRot_5(A)); inc(E,(D xor (B and (C xor D))) + W[i ] + K1); asm db $66; rol word[B],30 end; |
| 476 |
inc(D,LRot_5(E)); inc(D,(C xor (A and (B xor C))) + W[i+1] + K1); asm db $66; rol word[A],30 end; |
| 477 |
inc(C,LRot_5(D)); inc(C,(B xor (E and (A xor B))) + W[i+2] + K1); asm db $66; rol word[E],30 end; |
| 478 |
inc(B,LRot_5(C)); inc(B,(A xor (D and (E xor A))) + W[i+3] + K1); asm db $66; rol word[D],30 end; |
| 479 |
inc(A,LRot_5(B)); inc(A,(E xor (C and (D xor E))) + W[i+4] + K1); asm db $66; rol word[C],30 end; |
| 480 |
inc(i,5); |
| 481 |
end; |
| 482 |
while i<40 do begin |
| 483 |
inc(E,LRot_5(A)); inc(E,(B xor C xor D) + W[i ] + K2); asm db $66; rol word[B],30 end; |
| 484 |
inc(D,LRot_5(E)); inc(D,(A xor B xor C) + W[i+1] + K2); asm db $66; rol word[A],30 end; |
| 485 |
inc(C,LRot_5(D)); inc(C,(E xor A xor B) + W[i+2] + K2); asm db $66; rol word[E],30 end; |
| 486 |
inc(B,LRot_5(C)); inc(B,(D xor E xor A) + W[i+3] + K2); asm db $66; rol word[D],30 end; |
| 487 |
inc(A,LRot_5(B)); inc(A,(C xor D xor E) + W[i+4] + K2); asm db $66; rol word[C],30 end; |
| 488 |
inc(i,5); |
| 489 |
end; |
| 490 |
while i<60 do begin |
| 491 |
inc(E,LRot_5(A)); inc(E,((B and C) or (D and (B or C))) + W[i ] + K3); asm db $66; rol word[B],30 end; |
| 492 |
inc(D,LRot_5(E)); inc(D,((A and B) or (C and (A or B))) + W[i+1] + K3); asm db $66; rol word[A],30 end; |
| 493 |
inc(C,LRot_5(D)); inc(C,((E and A) or (B and (E or A))) + W[i+2] + K3); asm db $66; rol word[E],30 end; |
| 494 |
inc(B,LRot_5(C)); inc(B,((D and E) or (A and (D or E))) + W[i+3] + K3); asm db $66; rol word[D],30 end; |
| 495 |
inc(A,LRot_5(B)); inc(A,((C and D) or (E and (C or D))) + W[i+4] + K3); asm db $66; rol word[C],30 end; |
| 496 |
inc(i,5); |
| 497 |
end; |
| 498 |
while i<80 do begin |
| 499 |
inc(E,LRot_5(A)); inc(E,(B xor C xor D) + W[i ] + K4); asm db $66; rol word[B],30 end; |
| 500 |
inc(D,LRot_5(E)); inc(D,(A xor B xor C) + W[i+1] + K4); asm db $66; rol word[A],30 end; |
| 501 |
inc(C,LRot_5(D)); inc(C,(E xor A xor B) + W[i+2] + K4); asm db $66; rol word[E],30 end; |
| 502 |
inc(B,LRot_5(C)); inc(B,(D xor E xor A) + W[i+3] + K4); asm db $66; rol word[D],30 end; |
| 503 |
inc(A,LRot_5(B)); inc(A,(C xor D xor E) + W[i+4] + K4); asm db $66; rol word[C],30 end; |
| 504 |
inc(i,5); |
| 505 |
end; |
| 506 |
|
| 507 |
{Calculate new working hash} |
| 508 |
inc(Data.Hash[0], A); |
| 509 |
inc(Data.Hash[1], B); |
| 510 |
inc(Data.Hash[2], C); |
| 511 |
inc(Data.Hash[3], D); |
| 512 |
inc(Data.Hash[4], E); |
| 513 |
|
| 514 |
end; |
| 515 |
|
| 516 |
|
| 517 |
{$else} |
| 518 |
|
| 519 |
{TP5/5.5} |
| 520 |
|
| 521 |
{---------------------------------------------------------------------------} |
| 522 |
procedure UpdateLen(var whi, wlo: longint; BLen: longint); |
| 523 |
{-Add BLen to 64 bit value (wlo, whi)} |
| 524 |
inline( |
| 525 |
$58/ {pop ax } |
| 526 |
$5A/ {pop dx } |
| 527 |
$5B/ {pop bx } |
| 528 |
$07/ {pop es } |
| 529 |
$26/$01/$07/ {add es:[bx],ax } |
| 530 |
$26/$11/$57/$02/ {adc es:[bx+02],dx} |
| 531 |
$5B/ {pop bx } |
| 532 |
$07/ {pop es } |
| 533 |
$26/$83/$17/$00/ {adc es:[bx],0 } |
| 534 |
$26/$83/$57/$02/$00);{adc es:[bx+02],0 } |
| 535 |
|
| 536 |
|
| 537 |
{---------------------------------------------------------------------------} |
| 538 |
function RB(A: longint): longint; |
| 539 |
{-reverse byte order in longint} |
| 540 |
inline( |
| 541 |
$58/ { pop ax } |
| 542 |
$5A/ { pop dx } |
| 543 |
$86/$C6/ { xchg dh,al} |
| 544 |
$86/$E2); { xchg dl,ah} |
| 545 |
|
| 546 |
|
| 547 |
{---------------------------------------------------------------------------} |
| 548 |
function LRot_1(x: longint): longint; |
| 549 |
{-Rotate left 1} |
| 550 |
inline( |
| 551 |
$58/ { pop ax } |
| 552 |
$5A/ { pop dx } |
| 553 |
$2B/$C9/ { sub cx,cx} |
| 554 |
$D1/$D0/ { rcl ax,1 } |
| 555 |
$D1/$D2/ { rcl dx,1 } |
| 556 |
$13/$C1); { adc ax,cx} |
| 557 |
|
| 558 |
|
| 559 |
{---------------------------------------------------------------------------} |
| 560 |
function LRot_5(x: longint): longint; |
| 561 |
{-Rotate left 5} |
| 562 |
inline( |
| 563 |
$58/ { pop ax } |
| 564 |
$5A/ { pop dx } |
| 565 |
$2B/$C9/ { sub cx,cx} |
| 566 |
$D1/$D0/ { rcl ax,1 } |
| 567 |
$D1/$D2/ { rcl dx,1 } |
| 568 |
$13/$C1/ { adc ax,cx} |
| 569 |
$D1/$D0/ { rcl ax,1 } |
| 570 |
$D1/$D2/ { rcl dx,1 } |
| 571 |
$13/$C1/ { adc ax,cx} |
| 572 |
$D1/$D0/ { rcl ax,1 } |
| 573 |
$D1/$D2/ { rcl dx,1 } |
| 574 |
$13/$C1/ { adc ax,cx} |
| 575 |
$D1/$D0/ { rcl ax,1 } |
| 576 |
$D1/$D2/ { rcl dx,1 } |
| 577 |
$13/$C1/ { adc ax,cx} |
| 578 |
$D1/$D0/ { rcl ax,1 } |
| 579 |
$D1/$D2/ { rcl dx,1 } |
| 580 |
$13/$C1); { adc ax,cx} |
| 581 |
|
| 582 |
|
| 583 |
{---------------------------------------------------------------------------} |
| 584 |
function LRot_30(x: longint): longint; |
| 585 |
{-Rotate left 30 = rot right 2} |
| 586 |
inline( |
| 587 |
$58/ { pop ax } |
| 588 |
$5A/ { pop dx } |
| 589 |
$8B/$CA/ { mov cx,dx} |
| 590 |
$D1/$E9/ { shr cx,1 } |
| 591 |
$D1/$D8/ { rcr ax,1 } |
| 592 |
$D1/$DA/ { rcr dx,1 } |
| 593 |
$8B/$CA/ { mov cx,dx} |
| 594 |
$D1/$E9/ { shr cx,1 } |
| 595 |
$D1/$D8/ { rcr ax,1 } |
| 596 |
$D1/$DA); { rcr dx,1 } |
| 597 |
|
| 598 |
|
| 599 |
{---------------------------------------------------------------------------} |
| 600 |
procedure ExpandMessageBlocks(var W: TWorkBuf; var Buf: THashBuffer); |
| 601 |
{-Calculate "expanded message blocks"} |
| 602 |
var |
| 603 |
i: integer; |
| 604 |
begin |
| 605 |
{Part 1: Transfer buffer with little -> big endian conversion} |
| 606 |
for i:= 0 to 15 do W[i]:= RB(THashBuf32(Buf)[i]); |
| 607 |
{Part 2: Calculate remaining "expanded message blocks"} |
| 608 |
for i:= 16 to 79 do W[i]:= LRot_1(W[i-3] xor W[i-8] xor W[i-14] xor W[i-16]); |
| 609 |
end; |
| 610 |
|
| 611 |
|
| 612 |
{---------------------------------------------------------------------------} |
| 613 |
procedure SHA1Compress(var Data: THashContext); |
| 614 |
{-Actual hashing function} |
| 615 |
var |
| 616 |
i: integer; |
| 617 |
A, B, C, D, E: longint; |
| 618 |
W: TWorkBuf; |
| 619 |
begin |
| 620 |
ExpandMessageBlocks(W, Data.Buffer); |
| 621 |
|
| 622 |
{Assign old working hash to variables A..E} |
| 623 |
A := Data.Hash[0]; |
| 624 |
B := Data.Hash[1]; |
| 625 |
C := Data.Hash[2]; |
| 626 |
D := Data.Hash[3]; |
| 627 |
E := Data.Hash[4]; |
| 628 |
|
| 629 |
{SHA1 compression function} |
| 630 |
{Partial unroll for more speed, full unroll only marginally faster} |
| 631 |
{BIT16: rotateleft via function call} |
| 632 |
i := 0; |
| 633 |
while i<20 do begin |
| 634 |
inc(E,LRot_5(A) + (D xor (B and (C xor D))) + W[i ] + K1); B := LRot_30(B); |
| 635 |
inc(D,LRot_5(E) + (C xor (A and (B xor C))) + W[i+1] + K1); A := LRot_30(A); |
| 636 |
inc(C,LRot_5(D) + (B xor (E and (A xor B))) + W[i+2] + K1); E := LRot_30(E); |
| 637 |
inc(B,LRot_5(C) + (A xor (D and (E xor A))) + W[i+3] + K1); D := LRot_30(D); |
| 638 |
inc(A,LRot_5(B) + (E xor (C and (D xor E))) + W[i+4] + K1); C := LRot_30(C); |
| 639 |
inc(i,5); |
| 640 |
end; |
| 641 |
while i<40 do begin |
| 642 |
inc(E,LRot_5(A) + (B xor C xor D) + W[i ] + K2); B := LRot_30(B); |
| 643 |
inc(D,LRot_5(E) + (A xor B xor C) + W[i+1] + K2); A := LRot_30(A); |
| 644 |
inc(C,LRot_5(D) + (E xor A xor B) + W[i+2] + K2); E := LRot_30(E); |
| 645 |
inc(B,LRot_5(C) + (D xor E xor A) + W[i+3] + K2); D := LRot_30(D); |
| 646 |
inc(A,LRot_5(B) + (C xor D xor E) + W[i+4] + K2); C := LRot_30(C); |
| 647 |
inc(i,5); |
| 648 |
end; |
| 649 |
while i<60 do begin |
| 650 |
inc(E,LRot_5(A) + ((B and C) or (D and (B or C))) + W[i ] + K3); B := LRot_30(B); |
| 651 |
inc(D,LRot_5(E) + ((A and B) or (C and (A or B))) + W[i+1] + K3); A := LRot_30(A); |
| 652 |
inc(C,LRot_5(D) + ((E and A) or (B and (E or A))) + W[i+2] + K3); E := LRot_30(E); |
| 653 |
inc(B,LRot_5(C) + ((D and E) or (A and (D or E))) + W[i+3] + K3); D := LRot_30(D); |
| 654 |
inc(A,LRot_5(B) + ((C and D) or (E and (C or D))) + W[i+4] + K3); C := LRot_30(C); |
| 655 |
inc(i,5); |
| 656 |
end; |
| 657 |
while i<80 do begin |
| 658 |
inc(E,LRot_5(A) + (B xor C xor D) + W[i ] + K4); B := LRot_30(B); |
| 659 |
inc(D,LRot_5(E) + (A xor B xor C) + W[i+1] + K4); A := LRot_30(A); |
| 660 |
inc(C,LRot_5(D) + (E xor A xor B) + W[i+2] + K4); E := LRot_30(E); |
| 661 |
inc(B,LRot_5(C) + (D xor E xor A) + W[i+3] + K4); D := LRot_30(D); |
| 662 |
inc(A,LRot_5(B) + (C xor D xor E) + W[i+4] + K4); C := LRot_30(C); |
| 663 |
inc(i,5); |
| 664 |
end; |
| 665 |
|
| 666 |
{Calculate new working hash} |
| 667 |
inc(Data.Hash[0], A); |
| 668 |
inc(Data.Hash[1], B); |
| 669 |
inc(Data.Hash[2], C); |
| 670 |
inc(Data.Hash[3], D); |
| 671 |
inc(Data.Hash[4], E); |
| 672 |
|
| 673 |
end; |
| 674 |
|
| 675 |
{$endif BASM16} |
| 676 |
|
| 677 |
{$endif BIT16} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
{---------------------------------------------------------------------------} |
| 682 |
procedure SHA1Init(var Context: THashContext); |
| 683 |
{-initialize context} |
| 684 |
begin |
| 685 |
{Clear context, buffer=0!!} |
| 686 |
fillchar(Context,sizeof(Context),0); |
| 687 |
with Context do begin |
| 688 |
Hash[0] := longint($67452301); |
| 689 |
Hash[1] := longint($EFCDAB89); |
| 690 |
Hash[2] := longint($98BADCFE); |
| 691 |
Hash[3] := longint($10325476); |
| 692 |
Hash[4] := longint($C3D2E1F0); |
| 693 |
end; |
| 694 |
end; |
| 695 |
|
| 696 |
|
| 697 |
{---------------------------------------------------------------------------} |
| 698 |
procedure SHA1UpdateXL(var Context: THashContext; Msg: pointer; Len: longint); |
| 699 |
{-update context with Msg data} |
| 700 |
var |
| 701 |
i: integer; |
| 702 |
begin |
| 703 |
{Update message bit length} |
| 704 |
if Len<=$1FFFFFFF then UpdateLen(Context.MLen[1], Context.MLen[0], Len shl 3) |
| 705 |
else begin |
| 706 |
for i:=1 to 8 do UpdateLen(Context.MLen[1], Context.MLen[0], Len) |
| 707 |
end; |
| 708 |
while Len > 0 do begin |
| 709 |
{fill block with msg data} |
| 710 |
Context.Buffer[Context.Index]:= pByte(Msg)^; |
| 711 |
inc(Ptr2Inc(Msg)); |
| 712 |
inc(Context.Index); |
| 713 |
dec(Len); |
| 714 |
if Context.Index=SHA1_BlockLen then begin |
| 715 |
{If 512 bit transferred, compress a block} |
| 716 |
Context.Index:= 0; |
| 717 |
SHA1Compress(Context); |
| 718 |
while Len>=SHA1_BlockLen do begin |
| 719 |
move(Msg^,Context.Buffer,SHA1_BlockLen); |
| 720 |
SHA1Compress(Context); |
| 721 |
inc(Ptr2Inc(Msg),SHA1_BlockLen); |
| 722 |
dec(Len,SHA1_BlockLen); |
| 723 |
end; |
| 724 |
end; |
| 725 |
end; |
| 726 |
end; |
| 727 |
|
| 728 |
|
| 729 |
{---------------------------------------------------------------------------} |
| 730 |
procedure SHA1Update(var Context: THashContext; Msg: pointer; Len: word); |
| 731 |
{-update context with Msg data} |
| 732 |
begin |
| 733 |
SHA1UpdateXL(Context, Msg, Len); |
| 734 |
end; |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
{---------------------------------------------------------------------------} |
| 739 |
procedure SHA1FinalBitsEx(var Context: THashContext; var Digest: THashDigest; BData: byte; bitlen: integer); |
| 740 |
{-finalize SHA1 calculation with bitlen bits from BData (big-endian), clear context} |
| 741 |
var |
| 742 |
i: integer; |
| 743 |
begin |
| 744 |
{Message padding} |
| 745 |
{append bits from BData and a single '1' bit} |
| 746 |
if (bitlen>0) and (bitlen<=7) then begin |
| 747 |
Context.Buffer[Context.Index]:= (BData and BitAPI_Mask[bitlen]) or BitAPI_PBit[bitlen]; |
| 748 |
UpdateLen(Context.MLen[1], Context.MLen[0], bitlen); |
| 749 |
end |
| 750 |
else Context.Buffer[Context.Index]:= $80; |
| 751 |
|
| 752 |
for i:=Context.Index+1 to 63 do Context.Buffer[i] := 0; |
| 753 |
{2. Compress if more than 448 bits, (no room for 64 bit length} |
| 754 |
if Context.Index>= 56 then begin |
| 755 |
SHA1Compress(Context); |
| 756 |
fillchar(Context.Buffer,56,0); |
| 757 |
end; |
| 758 |
{Write 64 bit msg length into the last bits of the last block} |
| 759 |
{(in big endian format) and do a final compress} |
| 760 |
THashBuf32(Context.Buffer)[14] := RB(Context.MLen[1]); |
| 761 |
THashBuf32(Context.Buffer)[15] := RB(Context.MLen[0]); |
| 762 |
SHA1Compress(Context); |
| 763 |
{Hash->Digest to little endian format} |
| 764 |
fillchar(Digest, sizeof(Digest), 0); |
| 765 |
for i:=0 to 4 do THashDig32(Digest)[i]:= RB(Context.Hash[i]); |
| 766 |
{Clear context} |
| 767 |
fillchar(Context,sizeof(Context),0); |
| 768 |
end; |
| 769 |
|
| 770 |
|
| 771 |
{---------------------------------------------------------------------------} |
| 772 |
procedure SHA1FinalBits(var Context: THashContext; var Digest: TSHA1Digest; BData: byte; bitlen: integer); |
| 773 |
{-finalize SHA1 calculation with bitlen bits from BData (big-endian), clear context} |
| 774 |
var |
| 775 |
tmp: THashDigest; |
| 776 |
begin |
| 777 |
SHA1FinalBitsEx(Context, tmp, BData, bitlen); |
| 778 |
move(tmp, Digest, sizeof(Digest)); |
| 779 |
end; |
| 780 |
|
| 781 |
|
| 782 |
{---------------------------------------------------------------------------} |
| 783 |
procedure SHA1FinalEx(var Context: THashContext; var Digest: THashDigest); |
| 784 |
{-finalize SHA1 calculation, clear context} |
| 785 |
begin |
| 786 |
SHA1FinalBitsEx(Context,Digest,0,0); |
| 787 |
end; |
| 788 |
|
| 789 |
|
| 790 |
{---------------------------------------------------------------------------} |
| 791 |
procedure SHA1Final(var Context: THashContext; var Digest: TSHA1Digest); |
| 792 |
{-finalize SHA1 calculation, clear context} |
| 793 |
var |
| 794 |
tmp: THashDigest; |
| 795 |
begin |
| 796 |
SHA1FinalBitsEx(Context, tmp, 0, 0); |
| 797 |
move(tmp, Digest, sizeof(Digest)); |
| 798 |
end; |
| 799 |
|
| 800 |
|
| 801 |
{---------------------------------------------------------------------------} |
| 802 |
function SHA1SelfTest: boolean; |
| 803 |
{-self test SHA1: compare with known value} |
| 804 |
const |
| 805 |
s1: string[ 3] = 'abc'; |
| 806 |
s2: string[56] = 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'; |
| 807 |
D1: TSHA1Digest= ($a9,$99,$3e,$36,$47,$06,$81,$6a,$ba,$3e,$25,$71,$78,$50,$c2,$6c,$9c,$d0,$d8,$9d); |
| 808 |
D2: TSHA1Digest= ($84,$98,$3E,$44,$1C,$3B,$D2,$6E,$BA,$AE,$4A,$A1,$F9,$51,$29,$E5,$E5,$46,$70,$F1); |
| 809 |
D3: TSHA1Digest= ($bb,$6b,$3e,$18,$f0,$11,$5b,$57,$92,$52,$41,$67,$6f,$5b,$1a,$e8,$87,$47,$b0,$8a); |
| 810 |
D4: TSHA1Digest= ($98,$23,$2a,$15,$34,$53,$14,$9a,$f8,$d5,$2a,$61,$50,$3a,$50,$74,$b8,$59,$70,$e8); |
| 811 |
var |
| 812 |
Context: THashContext; |
| 813 |
Digest : TSHA1Digest; |
| 814 |
|
| 815 |
function SingleTest(s: Str127; TDig: TSHA1Digest): boolean; |
| 816 |
{-do a single test, const not allowed for VER<7} |
| 817 |
{ Two sub tests: 1. whole string, 2. one update per char} |
| 818 |
var |
| 819 |
i: integer; |
| 820 |
begin |
| 821 |
SingleTest := false; |
| 822 |
{1. Hash complete string} |
| 823 |
SHA1Full(Digest, @s[1],length(s)); |
| 824 |
{Compare with known value} |
| 825 |
if not HashSameDigest(@SHA1_Desc, PHashDigest(@Digest), PHashDigest(@TDig)) then exit; |
| 826 |
{2. one update call for all chars} |
| 827 |
SHA1Init(Context); |
| 828 |
for i:=1 to length(s) do SHA1Update(Context,@s[i],1); |
| 829 |
SHA1Final(Context,Digest); |
| 830 |
{Compare with known value} |
| 831 |
if not HashSameDigest(@SHA1_Desc, PHashDigest(@Digest), PHashDigest(@TDig)) then exit; |
| 832 |
SingleTest := true; |
| 833 |
end; |
| 834 |
|
| 835 |
begin |
| 836 |
SHA1SelfTest := false; |
| 837 |
{1 Zero bit from NESSIE test vectors} |
| 838 |
SHA1Init(Context); |
| 839 |
SHA1FinalBits(Context,Digest,0,1); |
| 840 |
if not HashSameDigest(@SHA1_Desc, PHashDigest(@Digest), PHashDigest(@D3)) then exit; |
| 841 |
{4 hightest bits of $50, D4 calculated with program shatest from RFC 4634} |
| 842 |
SHA1Init(Context); |
| 843 |
SHA1FinalBits(Context,Digest,$50,4); |
| 844 |
if not HashSameDigest(@SHA1_Desc, PHashDigest(@Digest), PHashDigest(@D4)) then exit; |
| 845 |
{strings from SHA1 document} |
| 846 |
SHA1SelfTest := SingleTest(s1, D1) and SingleTest(s2, D2) |
| 847 |
end; |
| 848 |
|
| 849 |
|
| 850 |
{---------------------------------------------------------------------------} |
| 851 |
procedure SHA1FullXL(var Digest: TSHA1Digest; Msg: pointer; Len: longint); |
| 852 |
{-SHA1 of Msg with init/update/final} |
| 853 |
var |
| 854 |
Context: THashContext; |
| 855 |
begin |
| 856 |
SHA1Init(Context); |
| 857 |
SHA1UpdateXL(Context, Msg, Len); |
| 858 |
SHA1Final(Context, Digest); |
| 859 |
end; |
| 860 |
|
| 861 |
|
| 862 |
{---------------------------------------------------------------------------} |
| 863 |
procedure SHA1Full(var Digest: TSHA1Digest; Msg: pointer; Len: word); |
| 864 |
{-SHA1 of Msg with init/update/final} |
| 865 |
begin |
| 866 |
SHA1FullXL(Digest, Msg, Len); |
| 867 |
end; |
| 868 |
|
| 869 |
|
| 870 |
{---------------------------------------------------------------------------} |
| 871 |
procedure SHA1File({$ifdef CONST} const {$endif} fname: Str255; |
| 872 |
var Digest: TSHA1Digest; var buf; bsize: word; var Err: word); |
| 873 |
{-SHA1 of file, buf: buffer with at least bsize bytes} |
| 874 |
var |
| 875 |
tmp: THashDigest; |
| 876 |
begin |
| 877 |
HashFile(fname, @SHA1_Desc, tmp, buf, bsize, Err); |
| 878 |
move(tmp, Digest, sizeof(Digest)); |
| 879 |
end; |
| 880 |
|
| 881 |
|
| 882 |
begin |
| 883 |
{$ifdef VER5X} |
| 884 |
fillchar(SHA1_Desc, sizeof(SHA1_Desc), 0); |
| 885 |
with SHA1_Desc do begin |
| 886 |
HSig := C_HashSig; |
| 887 |
HDSize := sizeof(THashDesc); |
| 888 |
HDVersion := C_HashVers; |
| 889 |
HBlockLen := SHA1_BlockLen; |
| 890 |
HDigestlen:= sizeof(TSHA1Digest); |
| 891 |
HInit := SHA1Init; |
| 892 |
HFinal := SHA1FinalEx; |
| 893 |
HUpdateXL := SHA1UpdateXL; |
| 894 |
HAlgNum := longint(_SHA1); |
| 895 |
HName := 'SHA1'; |
| 896 |
HPtrOID := @SHA1_OID; |
| 897 |
HLenOID := 6; |
| 898 |
HFinalBit := SHA1FinalBitsEx; |
| 899 |
end; |
| 900 |
{$endif} |
| 901 |
RegisterHash(_SHA1, @SHA1_Desc); |
| 902 |
end. |