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