FreeXP

CVS freexp

CVS User mw cvs-list at freexp.de
Son Nov 2 11:56:08 CET 2008


Update of /server/cvs/freexp
In directory m6s15:/tmp/cvs-serv23552

Modified Files:
	hash.pas md5.pas sha1.pas std.inc 
Log Message:
MW: - Neues Upstream-Release der Crypto-Lib

--- /server/cvs/freexp/hash.pas	2008/01/01 20:05:42	1.3
+++ /server/cvs/freexp/hash.pas	2008/11/02 10:56:08	1.4
@@ -1,4 +1,4 @@
-{ $Id: hash.pas,v 1.3 2008/01/01 20:05:42 mw Exp $ }
+{ $Id: hash.pas,v 1.4 2008/11/02 10:56:08 mw Exp $ }
 unit Hash;
 
 {General Hash Unit: This unit defines the common types, functions, and
@@ -40,10 +40,14 @@
  0.21     22.02.07  we          POID_Vec=^TOID_Vec, typed HPtrOID
  0.22     24.02.07  we          added some checks for HSig=C_HashSig
  0.23     04.10.07  we          THashContext.Index now longint
+ 0.24     02.05.08  we          type PHashDigest, function HashSameDigest
+ 0.25     04.05.08  we          BitAPI_Mask, BitAPI_PBit
+ 0.26     05.05.08  we          Descriptor with HFinalBit, C_HashVers=$00010004
+ 0.27     20.05.08  we          RMD160 as alias for RIPEMD160
 **************************************************************************)
 
 (*-------------------------------------------------------------------------
- (C) Copyright 2006-2007 Wolfgang Ehrhardt
+ (C) Copyright 2006-2008 Wolfgang Ehrhardt
 
  This software is provided 'as-is', without any express or implied warranty.
  In no event will the authors be held liable for any damages arising from
@@ -71,12 +75,15 @@
   THashAlgorithm = (_MD4,_MD5,_RIPEMD160,_SHA1,_SHA224,_SHA256,_SHA384,_SHA512, _Whirlpool); {Supported hash algorithms}
 
 const
+  _RMD160  = _RIPEMD160;     {Alias}
+
+const
   MaxBlockLen  = 128;        {Max. block length (buffer size), multiple of 4}
   MaxDigestLen = 64;         {Max. length of hash digest}
   MaxStateLen  = 16;         {Max. size of internal state}
   MaxOIDLen    = 9;          {Current max. OID length}
   C_HashSig    = $3D7A;      {Signature for Hash descriptor}
-  C_HashVers   = $00010003;  {Version of Hash definitions}
+  C_HashVers   = $00010004;  {Version of Hash definitions}
   C_MinHash    = _MD4;       {Lowest  hash in THashAlgorithm}
   C_MaxHash    = _Whirlpool; {Highest hash in THashAlgorithm}
 
@@ -84,6 +91,7 @@
   THashState   = packed array[0..MaxStateLen-1] of longint;         {Internal state}
   THashBuffer  = packed array[0..MaxBlockLen-1] of byte;            {hash buffer block}
   THashDigest  = packed array[0..MaxDigestLen-1] of byte;           {hash digest}
+  PHashDigest  = ^THashDigest;                                      {pointer to hash digest}
   THashBuf32   = packed array[0..MaxBlockLen  div 4 -1] of longint; {type cast helper}
   THashDig32   = packed array[0..MaxDigestLen div 4 -1] of longint; {type cast helper}
 
@@ -93,7 +101,7 @@
                    Hash  : THashState;             {Working hash}
                    MLen  : packed array[0..3] of longint; {max 128 bit msg length}
                    Buffer: THashBuffer;            {Block buffer}
-                   Index : logint;                 {Index in buffer}
+                   Index : longint;                {Index in buffer}
                  end;
 
 type
@@ -112,12 +120,16 @@
                       {-initialize context}
                       {$ifdef DLL} stdcall; {$endif}
 
+  HashUpdateXLProc = procedure(var Context: THashContext; Msg: pointer; Len: longint);
+                      {-update context with Msg data}
+                      {$ifdef DLL} stdcall; {$endif}
+
   HashFinalProc    = procedure(var Context: THashContext; var Digest: THashDigest);
                       {-finalize calculation, clear context}
                       {$ifdef DLL} stdcall; {$endif}
 
-  HashUpdateXLProc = procedure(var Context: THashContext; Msg: pointer; Len: longint);
-                      {-update context with Msg data}
+  HashFinalBitProc = procedure(var Context: THashContext; var Digest: THashDigest; BData: byte; bitlen: integer);
+                      {-finalize calculation with bitlen bits from BData, clear context}
                       {$ifdef DLL} stdcall; {$endif}
 
 type
@@ -140,10 +152,16 @@
                 HName     : THashName;         {Name of hash algo   }
                 HPtrOID   : POID_Vec;          {Pointer to OID vec  }
                 HLenOID   : word;              {Length of OID vec   }
-                HReserved : packed array[0..25] of byte;
+                HFill     : word;
+                HFinalBit : HashFinalBitProc;  {Bit-API Final proc  }
+                HReserved : packed array[0..19] of byte;
               end;
 
 
+const
+  BitAPI_Mask: array[0..7] of byte = ($00,$80,$C0,$E0,$F0,$F8,$FC,$FE);
+  BitAPI_PBit: array[0..7] of byte = ($80,$40,$20,$10,$08,$04,$02,$01);
+
 procedure RegisterHash(AlgId: THashAlgorithm; PHash: PHashDesc);
   {-Register algorithm with AlgID and Hash descriptor PHash^}
   {$ifdef DLL} stdcall; {$endif}
@@ -156,7 +174,6 @@
   {-Return PHashDesc of Algo with AlgoName, nil if not found/registered}
   {$ifdef DLL} stdcall; {$endif}
 
-
 procedure HashFile({$ifndef BIT16} const fname: shortstring; {$else} fname: string; {$endif}
                    PHash: PHashDesc; var Digest: THashDigest; var buf; bsize: word; var Err: word);
   {-Calulate hash digest of file, buf: buffer with at least bsize bytes}
@@ -174,6 +191,9 @@
   {-Calulate hash digest of Msg with init/update/final}
   {$ifdef DLL} stdcall; {$endif}
 
+function  HashSameDigest(PHash: PHashDesc; PD1, PD2: PHashDigest): boolean;
+  {-Return true if same digests, using HDigestlen of PHash}
+  {$ifdef DLL} stdcall; {$endif}
 
 
 implementation
@@ -229,6 +249,8 @@
 
 begin
   AlgoName := StrUpcase(Algoname);
+  {Transform RMD160 alias to standard name}
+  if AlgoName='RMD160' then AlgoName:='RIPEMD160';
   FindHash_by_Name := nil;
   for algo := C_MinHash to C_MaxHash do begin
     phash := PHashVec[algo];
@@ -277,6 +299,24 @@
 end;
 
 
+{---------------------------------------------------------------------------}
+function HashSameDigest(PHash: PHashDesc; PD1, PD2: PHashDigest): boolean;
+  {-Return true if same digests, using HDigestlen of PHash}
+var
+  i: integer;
+begin
+  HashSameDigest := false;
+  if PHash<>nil then with PHash^ do begin
+    if (HSig=C_HashSig) and (HDigestlen>0) then begin
+      for i:=0 to pred(HDigestlen) do begin
+        if PD1^[i]<>PD2^[i] then exit;
+      end;
+      HashSameDigest := true;
+    end;
+  end;
+end;
+
+
 {$i-} {Force I-}
 {---------------------------------------------------------------------------}
 procedure HashFile({$ifndef BIT16} const fname: shortstring; {$else} fname: string; {$endif}
@@ -334,7 +374,10 @@
 end.
 {
   $Log: hash.pas,v $
-  Revision 1.3  2008/01/01 20:05:42  mw
+  Revision 1.4  2008/11/02 10:56:08  mw
+  MW: - Neues Upstream-Release der Crypto-Lib
+
+  Revision 1.3  2008-01-01 20:05:42  mw
   MW: - Neues Upstream-Release der Cryptolib eingebaut.
 
   Revision 1.2  2007-07-15 09:17:55  mw
--- /server/cvs/freexp/md5.pas	2008/01/01 20:05:48	1.12
+++ /server/cvs/freexp/md5.pas	2008/11/02 10:56:08	1.13
@@ -1,4 +1,4 @@
-{ $Id: md5.pas,v 1.12 2008/01/01 20:05:48 mw Exp $ }
+{ $Id: md5.pas,v 1.13 2008/11/02 10:56:08 mw Exp $ }
 unit MD5;
 {$I-}
 {MD5 - 128 bit Hash function}
@@ -65,12 +65,15 @@
  3.12fxp1 15.07.07  fxp         Ported to new Upstream Release, with extra STD.INC
 
  3.13     04.10.07  we          FPC: {$asmmode intel}
+ 3.14     02.05.08  we          Bit-API: MD5FinalBits/Ex
+ 3.15     05.05.08  we          THashDesc constant with HFinalBit field
+
+ 3.15fxp1 02.11.08  fxp         Ported to new Upstream Release
 
- 3.13fxp1 01.01.08  fxp         Ported to new Upstream Release
 **************************************************************************)
 
 (*-------------------------------------------------------------------------
- (C) Copyright 2002-2007 Wolfgang Ehrhardt
+ (C) Copyright 2002-2008 Wolfgang Ehrhardt
  (C) Copyright 2006-2008 FreeXP
 
  This software is provided 'as-is', without any express or implied warranty.
@@ -126,6 +129,14 @@
   {-finalize MD5 calculation, clear context}
   {$ifdef DLL} stdcall; {$endif}
 
+procedure MD5FinalBitsEx(var Context: THashContext; var Digest: THashDigest; BData: byte; bitlen: integer);
+  {-finalize MD5 calculation with bitlen bits from BData (big-endian), clear context}
+  {$ifdef DLL} stdcall; {$endif}
+
+procedure MD5FinalBits(var Context: THashContext; var Digest: TMD5Digest; BData: byte; bitlen: integer);
+  {-finalize MD5 calculation with bitlen bits from BData (big-endian), clear context}
+  {$ifdef DLL} stdcall; {$endif}
+
 function  MD5SelfTest: boolean;
   {-self test for string from MD5 document}
   {$ifdef DLL} stdcall; {$endif}
@@ -147,10 +158,12 @@
 
 implementation
 
+
 {$ifdef FPC}
   {$asmmode intel}
 {$endif}
 
+
 {$ifdef BIT16}
   {$F-}
 {$endif}
@@ -163,6 +176,46 @@
 type
   PByte   = ^byte;
 
+{1.2.840.113549.2.5}
+{iso(1) member-body(2) us(840) rsadsi(113549) digestAlgorithm(2) md5(5)}
+const
+  MD5_OID : TOID_Vec = (1,2,840,113549,2,5,-1,-1,-1); {Len=6}
+
+
+{$ifndef VER5X}
+const
+  MD5_Desc: THashDesc = (
+               HSig      : C_HashSig;
+               HDSize    : sizeof(THashDesc);
+               HDVersion : C_HashVers;
+               HBlockLen : MD5_BlockLen;
+               HDigestlen: sizeof(TMD5Digest);
+             {$ifdef FPC_ProcVar}
+               HInit     : @MD5Init;
+               HFinal    : @MD5FinalEx;
+               HUpdateXL : @MD5UpdateXL;
+             {$else}
+               HInit     : MD5Init;
+               HFinal    : MD5FinalEx;
+               HUpdateXL : MD5UpdateXL;
+             {$endif}
+               HAlgNum   : longint(_MD5);
+               HName     : 'MD5';
+               HPtrOID   : @MD5_OID;
+               HLenOID   : 6;
+               HFill     : 0;
+             {$ifdef FPC_ProcVar}
+               HFinalBit : @MD5FinalBitsEx;
+             {$else}
+               HFinalBit : MD5FinalBitsEx;
+             {$endif}
+               HReserved : (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
+            );
+{$else}
+var
+  MD5_Desc: THashDesc;
+{$endif}
+
 
 {$ifdef StrictLong}
   {$warnings off}
@@ -557,14 +610,19 @@
 
 
 {---------------------------------------------------------------------------}
-procedure MD5FinalEx(var Context: THashContext; var Digest: THashDigest);
-  {-finalize MD5 calculation, clear context}
+procedure MD5FinalBitsEx(var Context: THashContext; var Digest: THashDigest; BData: byte; bitlen: integer);
+  {-finalize MD5 calculation with bitlen bits from BData (big-endian), clear context}
 var
   i: integer;
 begin
   {Message padding}
-  {1. append bit '1' after msg}
-  Context.Buffer[Context.Index]:= $80;
+  {append bits from BData and a single '1' bit}
+  if (bitlen>0) and (bitlen<=7) then begin
+    Context.Buffer[Context.Index]:= (BData and BitAPI_Mask[bitlen]) or BitAPI_PBit[bitlen];
+    UpdateLen(Context.MLen[1], Context.MLen[0], bitlen);
+  end
+  else Context.Buffer[Context.Index]:= $80;
+
   for i:=Context.Index+1 to 63 do Context.Buffer[i] := 0;
   {2. Compress if more than 448 bits, (no room for 64 bit length}
   if Context.Index>= 56 then begin
@@ -584,12 +642,30 @@
 
 
 {---------------------------------------------------------------------------}
+procedure MD5FinalBits(var Context: THashContext; var Digest: TMD5Digest; BData: byte; bitlen: integer);
+  {-finalize MD5 calculation with bitlen bits from BData (big-endian), clear context}
+var
+  tmp: THashDigest;
+begin
+  MD5FinalBitsEx(Context, tmp, BData, bitlen);
+  move(tmp, Digest, sizeof(Digest));
+end;
+
+
+{---------------------------------------------------------------------------}
+procedure MD5FinalEx(var Context: THashContext; var Digest: THashDigest);
+  {-finalize MD5 calculation, clear context}
+begin
+  MD5FinalBitsEx(Context,Digest,0,0);
+end;
+
+{---------------------------------------------------------------------------}
 procedure MD5Final(var Context: THashContext; var Digest: TMD5Digest);
   {-finalize MD5 calculation, clear context}
 var
   tmp: THashDigest;
 begin
-  MD5FinalEx(Context, tmp);
+  MD5FinalBitsEx(Context, tmp, 0, 0);
   move(tmp, Digest, sizeof(Digest));
 end;
 
@@ -602,41 +678,39 @@
   s2: string[62] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   D1: TMD5Digest= ($90,$01,$50,$98,$3c,$d2,$4f,$b0,$d6,$96,$3f,$7d,$28,$e1,$7f,$72);
   D2: TMD5Digest= ($d1,$74,$ab,$98,$d2,$77,$d9,$f5,$a5,$61,$1c,$2c,$9f,$41,$9d,$9f);
-
+  D3: TMD5Digest= ($1d,$a6,$35,$b1,$43,$0f,$17,$1c,$65,$72,$06,$fd,$69,$fe,$e0,$e8);
+var
+  Context: THashContext;
+  Digest : TMD5Digest;
 
   function SingleTest(s: string; TDig: TMD5Digest): boolean;
     {-do a single test, const not allowed for VER<7}
     { Two sub tests: 1. whole string, 2. one update per char}
   var
-    Context: THashContext;
-    Digest : TMD5Digest;
     i: integer;
   begin
+    SingleTest := false;
     {1. Hash complete string}
     MD5Full(Digest, @s[1],length(s));
     {Compare with known value}
-    for i:=0 to sizeof(Digest)-1 do begin
-      if Digest[i]<>TDig[i] then begin
-        SingleTest := false;
-        exit;
-      end;
-    end;
+    if not HashSameDigest(@MD5_Desc, PHashDigest(@Digest), PHashDigest(@TDig)) then exit;
     {2. one update call for all chars}
     MD5Init(Context);
     for i:=1 to length(s) do MD5Update(Context, at s[i],1);
     MD5Final(Context,Digest);
     {Compare with known value}
-    for i:=0 to sizeof(Digest)-1 do begin
-      if Digest[i]<>TDig[i] then begin
-        SingleTest := false;
-        exit;
-      end;
-    end;
+    if not HashSameDigest(@MD5_Desc, PHashDigest(@Digest), PHashDigest(@TDig)) then exit;
     SingleTest := true;
   end;
 
 begin
-  MD5SelfTest := SingleTest(s1, D1) and SingleTest(s2, D2);
+  MD5SelfTest := false;
+  {1 Zero bit from NESSIE test vectors}
+  MD5Init(Context);
+  MD5FinalBits(Context,Digest,0,1);
+  if not HashSameDigest(@MD5_Desc, PHashDigest(@Digest), PHashDigest(@D3)) then exit;
+  {strings from MD5 document}
+  MD5SelfTest := SingleTest(s1, D1) and SingleTest(s2, D2)
 end;
 
 
@@ -659,41 +733,6 @@
   MD5FullXL(Digest, Msg, Len);
 end;
 
-
-{1.2.840.113549.2.5}
-{iso(1) member-body(2) us(840) rsadsi(113549) digestAlgorithm(2) md5(5)}
-const
-  MD5_OID : TOID_Vec = (1,2,840,113549,2,5,-1,-1,-1); {Len=6}
-
-
-{$ifndef VER5X}
-const
-  MD5_Desc: THashDesc = (
-               HSig      : C_HashSig;
-               HDSize    : sizeof(THashDesc);
-               HDVersion : C_HashVers;
-               HBlockLen : MD5_BlockLen;
-               HDigestlen: sizeof(TMD5Digest);
-             {$ifdef FPC_ProcVar}
-               HInit     : @MD5Init;
-               HFinal    : @MD5FinalEx;
-               HUpdateXL : @MD5UpdateXL;
-             {$else}
-               HInit     : MD5Init;
-               HFinal    : MD5FinalEx;
-               HUpdateXL : MD5UpdateXL;
-             {$endif}
-               HAlgNum   : longint(_MD5);
-               HName     : 'MD5';
-               HPtrOID   : @MD5_OID;
-               HLenOID   : 6;
-               HReserved : (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
-            );
-{$else}
-var
-  MD5_Desc: THashDesc;
-{$endif}
-
 {---------------------------------------------------------------------------}
 procedure MD5File({$ifndef BIT16} const fname: shortstring; {$else} fname: string; {$endif}
                   var Digest: TMD5Digest; var buf; bsize: word; var Err: word);
@@ -722,13 +761,17 @@
        HName     := 'MD5';
        HPtrOID   := @MD5_OID;
        HLenOID   := 6;
+       HFinalBit := MD5FinalBitsEx;
     end;
   {$endif}
   RegisterHash(_MD5, @MD5_Desc);
 end.
 {
   $Log: md5.pas,v $
-  Revision 1.12  2008/01/01 20:05:48  mw
+  Revision 1.13  2008/11/02 10:56:08  mw
+  MW: - Neues Upstream-Release der Crypto-Lib
+
+  Revision 1.12  2008-01-01 20:05:48  mw
   MW: - Neues Upstream-Release der Cryptolib eingebaut.
 
   Revision 1.11  2007-07-15 09:17:55  mw
@@ -771,4 +814,4 @@
         automatisch aktualiert, sofern der Benutzer die Datei nicht
         modifiziert hat. Dabei wird soweit m”glich auch die Sprach-
         einstellung bercksichtig.
-}
+}
\ No newline at end of file
--- /server/cvs/freexp/sha1.pas	2008/01/01 20:05:49	1.6
+++ /server/cvs/freexp/sha1.pas	2008/11/02 10:56:08	1.7
@@ -1,4 +1,4 @@
-{ $Id: sha1.pas,v 1.6 2008/01/01 20:05:49 mw Exp $ }
+{ $Id: sha1.pas,v 1.7 2008/11/02 10:56:08 mw Exp $ }
 unit SHA1;
 {$I-}
 {SHA1 - 160 bit Secure Hash Function}
@@ -67,19 +67,19 @@
  3.14     26.03.06  we          Round constants K1..K4, code reordering
  3.15     07.08.06  we          $ifdef BIT32: (const fname: shortstring...)
  3.16     22.02.07  we          values for OID vector
- 3.17     30.06.07  we          Use conditional define FPC_ProcVar 
+ 3.17     30.06.07  we          Use conditional define FPC_ProcVar
 
  3.17fxp1 15.07.07  fxp         sha1sum_file und sha1sum_str implementiert
 
- 3.18     04.10.07  we          FPC: {$asmmode intel}
-
- 3.18fxp1 01.01.08  fxp         Uebernahme der Upstream-Aenderungen
+ 3.19     02.05.08  we          Bit-API: SHA1FinalBits/Ex
+ 3.20     05.05.08  we          THashDesc constant with HFinalBit field
 
+ 3.20fxp1 02.11.08  fxp         Neues Upstream Release
 **************************************************************************)
 
 
 (*-------------------------------------------------------------------------
- (C) Copyright 2002-2007 Wolfgang Ehrhardt
+ (C) Copyright 2002-2008 Wolfgang Ehrhardt
  (C) Copyright 2007-2008 FreeXP
 
  This software is provided 'as-is', without any express or implied warranty.
@@ -110,7 +110,7 @@
 uses {$IFDEF FreeXP}typeform,fileio,{$ELSE}freexp,{$ENDIF}hash;
 
 type sha1str  = string[40];
-
+ 
 function sha1sum_file(const source:string):sha1str;
   {-initialize context}
   {$ifdef DLL} stdcall; {$endif}
@@ -139,8 +139,16 @@
   {-finalize SHA1 calculation, clear context}
   {$ifdef DLL} stdcall; {$endif}
 
+procedure SHA1FinalBitsEx(var Context: THashContext; var Digest: THashDigest; BData: byte; bitlen: integer);
+  {-finalize SHA1 calculation with bitlen bits from BData (big-endian), clear context}
+  {$ifdef DLL} stdcall; {$endif}
+
+procedure SHA1FinalBits(var Context: THashContext; var Digest: TSHA1Digest; BData: byte; bitlen: integer);
+  {-finalize SHA1 calculation with bitlen bits from BData (big-endian), clear context}
+  {$ifdef DLL} stdcall; {$endif}
+
 function  SHA1SelfTest: boolean;
-  {-self test for string from SHA1 document}
+  {-self test SHA1: compare with known value}
   {$ifdef DLL} stdcall; {$endif}
 
 procedure SHA1Full(var Digest: TSHA1Digest; Msg: pointer; Len: word);
@@ -159,9 +167,6 @@
 
 implementation
 
-{$ifdef FPC}
-  {$asmmode intel}
-{$endif}
 
 {$ifdef BIT16}
   {$F-}
@@ -182,74 +187,44 @@
   TWorkBuf = array[0..79] of longint;
   PByte    = ^byte;
 
-function sha1sum_file(const source:string):sha1str;
-type sha1buf = array[1..$FFFF] of byte;
-var  f         : file;
-     context   : THashContext;
-     digest    : TSHA1Digest;
-     StrDigest : sha1str;
-     l         : longint;
-     n         : word;
-     w         : byte;
-     err       : integer;
-     sha1bufp   : ^sha1buf;
-begin
-  sha1sum_file:='';
-  New(sha1bufp);
-  SHA1Init(context);
-  w:=FileMode;
-  fm_ro;
-  assign(f,source);
-  reset(f,1);
-  err:=IOResult;
-  FileMode:=w;
-  if err<>0 then begin
-    Dispose(sha1bufp);
-    exit;
-  end;
-  l:=FileSize(f);
-  if IOResult<>0 then begin
-     Dispose(sha1bufp);
-     exit;
-  end;
-  while (err=0) and (l>0) do
-  begin
-    blockread(f,sha1bufp^,SizeOf(sha1buf),n);
-    err:=IOResult;
-    dec(l,n);
-    SHA1Update(context,sha1bufp,n);
-  end;
-  close(f);
-  if IOResult=0 then;
-  if err<>0 then begin
-    Dispose(sha1bufp);
-    exit;
-  end;
-  SHA1Final(context,digest);
-  Dispose(sha1bufp);
-  StrDigest:='';
-  for n:=0 to 19 do StrDigest:=StrDigest+hex(digest[n],2);
-  sha1sum_file:=StrDigest;
-end;
-
+{1.3.14.3.2.26}
+{iso(1) identified-organization(3) oiw(14) secsig(3) algorithms(2) hashAlgorithmIdentifier(26)}
+const
+  SHA1_OID : TOID_Vec = (1,3,14,3,2,26,-1,-1,-1); {Len=6}
 
-function sha1sum_str(const source:string):sha1str;
-var context   : THashContext;
-    digest    : TSHA1Digest;
-    sha1buf    : array[1..255] of byte;
-    StrDigest : sha1str;
-    l         : byte;
-begin
-  sha1sum_str:='';
-  SHA1Init(context);
-  l:=length(source);
-  move(source[1],sha1buf[1],l);
-  SHA1Update(context, at sha1buf,l);
-  SHA1Final(context,digest);
-  StrDigest:='';
-  for l:=0 to 19 do StrDigest:=StrDigest+hex(digest[l],2);
-  sha1sum_str:=StrDigest;
-end;
+{$ifndef VER5X}
+const
+  SHA1_Desc: THashDesc = (
+               HSig      : C_HashSig;
+               HDSize    : sizeof(THashDesc);
+               HDVersion : C_HashVers;
+               HBlockLen : SHA1_BlockLen;
+               HDigestlen: sizeof(TSHA1Digest);
+             {$ifdef FPC_ProcVar}
+               HInit     : @SHA1Init;
+               HFinal    : @SHA1FinalEx;
+               HUpdateXL : @SHA1UpdateXL;
+             {$else}
+               HInit     : SHA1Init;
+               HFinal    : SHA1FinalEx;
+               HUpdateXL : SHA1UpdateXL;
+             {$endif}
+               HAlgNum   : longint(_SHA1);
+               HName     : 'SHA1';
+               HPtrOID   : @SHA1_OID;
+               HLenOID   : 6;
+               HFill     : 0;
+             {$ifdef FPC_ProcVar}
+               HFinalBit : @SHA1FinalBitsEx;
+             {$else}
+               HFinalBit : SHA1FinalBitsEx;
+             {$endif}
+               HReserved : (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
+            );
+{$else}
+var
+  SHA1_Desc: THashDesc;
+{$endif}
 
 
 {$ifdef BIT32}
@@ -679,6 +654,73 @@
 
 {$endif BIT32}
 
+function sha1sum_file(const source:string):sha1str;
+type sha1buf = array[1..$FFFF] of byte;
+var  f         : file;
+     context   : THashContext;
+     digest    : TSHA1Digest;
+     StrDigest : sha1str;
+     l         : longint;
+     n         : word;
+     w         : byte;
+     err       : integer;
+     sha1bufp   : ^sha1buf;
+begin
+  sha1sum_file:='';
+  New(sha1bufp);
+  SHA1Init(context);
+  w:=FileMode;
+  fm_ro;
+  assign(f,source);
+  reset(f,1);
+  err:=IOResult;
+  FileMode:=w;
+  if err<>0 then begin
+    Dispose(sha1bufp);
+    exit;
+  end;
+  l:=FileSize(f);
+  if IOResult<>0 then begin
+     Dispose(sha1bufp);
+     exit;
+  end;
+  while (err=0) and (l>0) do
+  begin
+    blockread(f,sha1bufp^,SizeOf(sha1buf),n);
+    err:=IOResult;
+    dec(l,n);
+    SHA1Update(context,sha1bufp,n);
+  end;
+  close(f);
+  if IOResult=0 then;
+  if err<>0 then begin
+    Dispose(sha1bufp);
+    exit;
+  end;
+  SHA1Final(context,digest);
+  Dispose(sha1bufp);
+  StrDigest:='';
+  for n:=0 to 19 do StrDigest:=StrDigest+hex(digest[n],2);
+  sha1sum_file:=StrDigest;
+end;
+
+function sha1sum_str(const source:string):sha1str;
+var context   : THashContext;
+    digest    : TSHA1Digest;
+    sha1buf    : array[1..255] of byte;
+    StrDigest : sha1str;
+    l         : byte;
+begin
+  sha1sum_str:='';
+  SHA1Init(context);
+  l:=length(source);
+  move(source[1],sha1buf[1],l);
+  SHA1Update(context, at sha1buf,l);
+  SHA1Final(context,digest);
+  StrDigest:='';
+  for l:=0 to 19 do StrDigest:=StrDigest+hex(digest[l],2);
+  sha1sum_str:=StrDigest;
+end;
 
 
 {---------------------------------------------------------------------------}
@@ -738,14 +780,19 @@
 
 
 {---------------------------------------------------------------------------}
-procedure SHA1FinalEx(var Context: THashContext; var Digest: THashDigest);
-  {-finalize SHA1 calculation, clear context}
+procedure SHA1FinalBitsEx(var Context: THashContext; var Digest: THashDigest; BData: byte; bitlen: integer);
+  {-finalize SHA1 calculation with bitlen bits from BData (big-endian), clear context}
 var
   i: integer;
 begin
   {Message padding}
-  {1. append bit '1' after msg}
-  Context.Buffer[Context.Index]:= $80;
+  {append bits from BData and a single '1' bit}
+  if (bitlen>0) and (bitlen<=7) then begin
+    Context.Buffer[Context.Index]:= (BData and BitAPI_Mask[bitlen]) or BitAPI_PBit[bitlen];
+    UpdateLen(Context.MLen[1], Context.MLen[0], bitlen);
+  end
+  else Context.Buffer[Context.Index]:= $80;
+
   for i:=Context.Index+1 to 63 do Context.Buffer[i] := 0;
   {2. Compress if more than 448 bits, (no room for 64 bit length}
   if Context.Index>= 56 then begin
@@ -766,59 +813,81 @@
 
 
 {---------------------------------------------------------------------------}
+procedure SHA1FinalBits(var Context: THashContext; var Digest: TSHA1Digest; BData: byte; bitlen: integer);
+  {-finalize SHA1 calculation with bitlen bits from BData (big-endian), clear context}
+var
+  tmp: THashDigest;
+begin
+  SHA1FinalBitsEx(Context, tmp, BData, bitlen);
+  move(tmp, Digest, sizeof(Digest));
+end;
+
+
+{---------------------------------------------------------------------------}
+procedure SHA1FinalEx(var Context: THashContext; var Digest: THashDigest);
+  {-finalize SHA1 calculation, clear context}
+begin
+  SHA1FinalBitsEx(Context,Digest,0,0);
+end;
+
+
+{---------------------------------------------------------------------------}
 procedure SHA1Final(var Context: THashContext; var Digest: TSHA1Digest);
   {-finalize SHA1 calculation, clear context}
 var
   tmp: THashDigest;
 begin
-  SHA1FinalEx(Context, tmp);
+  SHA1FinalBitsEx(Context, tmp, 0, 0);
   move(tmp, Digest, sizeof(Digest));
 end;
 
 
 {---------------------------------------------------------------------------}
 function SHA1SelfTest: boolean;
-  {-self test for string from SHA1 document}
+  {-self test SHA1: compare with known value}
 const
   s1: string[ 3] = 'abc';
   s2: string[56] = 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq';
   D1: TSHA1Digest= ($a9,$99,$3e,$36,$47,$06,$81,$6a,$ba,$3e,$25,$71,$78,$50,$c2,$6c,$9c,$d0,$d8,$9d);
   D2: TSHA1Digest= ($84,$98,$3E,$44,$1C,$3B,$D2,$6E,$BA,$AE,$4A,$A1,$F9,$51,$29,$E5,$E5,$46,$70,$F1);
-
+  D3: TSHA1Digest= ($bb,$6b,$3e,$18,$f0,$11,$5b,$57,$92,$52,$41,$67,$6f,$5b,$1a,$e8,$87,$47,$b0,$8a);
+  D4: TSHA1Digest= ($98,$23,$2a,$15,$34,$53,$14,$9a,$f8,$d5,$2a,$61,$50,$3a,$50,$74,$b8,$59,$70,$e8);
+var
+  Context: THashContext;
+  Digest : TSHA1Digest;
 
   function SingleTest(s: string; TDig: TSHA1Digest): boolean;
     {-do a single test, const not allowed for VER<7}
     { Two sub tests: 1. whole string, 2. one update per char}
   var
-    Context: THashContext;
-    Digest: TSHA1Digest;
     i: integer;
   begin
+    SingleTest := false;
     {1. Hash complete string}
     SHA1Full(Digest, @s[1],length(s));
     {Compare with known value}
-    for i:=0 to sizeof(Digest)-1 do begin
-      if Digest[i]<>TDig[i] then begin
-        SingleTest := false;
-        exit;
-      end;
-    end;
+    if not HashSameDigest(@SHA1_Desc, PHashDigest(@Digest), PHashDigest(@TDig)) then exit;
     {2. one update call for all chars}
     SHA1Init(Context);
     for i:=1 to length(s) do SHA1Update(Context, at s[i],1);
     SHA1Final(Context,Digest);
     {Compare with known value}
-    for i:=0 to sizeof(Digest)-1 do begin
-      if Digest[i]<>TDig[i] then begin
-        SingleTest := false;
-        exit;
-      end;
-    end;
+    if not HashSameDigest(@SHA1_Desc, PHashDigest(@Digest), PHashDigest(@TDig)) then exit;
     SingleTest := true;
   end;
 
 begin
-  SHA1SelfTest := SingleTest(s1, D1) and SingleTest(s2, D2);
+  SHA1SelfTest := false;
+  {1 Zero bit from NESSIE test vectors}
+  SHA1Init(Context);
+  SHA1FinalBits(Context,Digest,0,1);
+  if not HashSameDigest(@SHA1_Desc, PHashDigest(@Digest), PHashDigest(@D3)) then exit;
+  {4 hightest bits of $50, D4 calculated with program shatest from RFC 4634}
+  SHA1Init(Context);
+  SHA1FinalBits(Context,Digest,$50,4);
+  if not HashSameDigest(@SHA1_Desc, PHashDigest(@Digest), PHashDigest(@D4)) then exit;
+  {strings from SHA1 document}
+  SHA1SelfTest := SingleTest(s1, D1) and SingleTest(s2, D2)
 end;
 
 
@@ -842,39 +911,6 @@
 end;
 
 
-{1.3.14.3.2.26}
-{iso(1) identified-organization(3) oiw(14) secsig(3) algorithms(2) hashAlgorithmIdentifier(26)}
-const
-  SHA1_OID : TOID_Vec = (1,3,14,3,2,26,-1,-1,-1); {Len=6}
-
-{$ifndef VER5X}
-const
-  SHA1_Desc: THashDesc = (
-               HSig      : C_HashSig;
-               HDSize    : sizeof(THashDesc);
-               HDVersion : C_HashVers;
-               HBlockLen : SHA1_BlockLen;
-               HDigestlen: sizeof(TSHA1Digest);
-             {$ifdef FPC_ProcVar}
-               HInit     : @SHA1Init;
-               HFinal    : @SHA1FinalEx;

[39 lines skipped]
--- /server/cvs/freexp/std.inc	2008/01/01 20:05:49	1.2
+++ /server/cvs/freexp/std.inc	2008/11/02 10:56:08	1.3
@@ -1,4 +1,4 @@
-{ $Id: std.inc,v 1.2 2008/01/01 20:05:49 mw Exp $ }
+{ $Id: std.inc,v 1.3 2008/11/02 10:56:08 mw Exp $ }
 (*************************************************************************
 
  DESCRIPTION     :  Standard definitions and options
@@ -40,11 +40,14 @@
  1.30     23.06.07  we          FPC_ProcVar: Helper for procedure variables
  1.31     18.09.07  we          HAS_INLINE for FPC VER2
  1.32     04.10.07  we          FPC Intel ASMmode only if CPUI386 is defined
+ 1.33     22.11.07  we          Record value of $X option, undef RESULT if $X-
+ 1.34     19.05.08  we          HAS_UINT64
+ 1.35     21.06.08  we          V7PLUS, HAS_UINT64 for FPC VER2_2
 **************************************************************************)
 
 
 (*-------------------------------------------------------------------------
- (C) Copyright 2002-2007 Wolfgang Ehrhardt
+ (C) Copyright 2002-2008 Wolfgang Ehrhardt
 
  This software is provided 'as-is', without any express or implied warranty.
  In no event will the authors be held liable for any damages arising from
@@ -69,31 +72,33 @@
 
 {$define _STD_INC_}   {include STD.INC only once}
 
-{.$undef  BIT16}      {16 Bit code, Pascal / D1}
-{.$undef  BIT32}      {32 Bit code}
-{.$undef  DELPHI}     {Delphi2+ and BCB++}
-{.$undef  G_OPT}      {G+ option support}
-{.$undef  D4PLUS}     {Delphi 4 and higher}
-{.$undef  BASM16}     {16 Bit BASM}
-{.$undef  LoadArgs}   {Register params}
-{.$undef  WINCRT}     {Use WinCRT for console}
-{.$undef  RESULT}     {Result pseudo variable}
-{.$undef  StrictLong} {Warning for longint const with MS bit}
-{.$undef  HAS_INT64}  {int64 integer type available}
-{.$undef  HAS_MSG}    {Has message directive}
-{.$undef  HAS_INLINE} {Has inline procs/funcs (D9)}
-{.$undef  ABSTRACT}   {Has abstract methods}
-{.$undef  DEFAULT}    {Support default parameters}
-{.$undef  VER5X}      {TP5 or TP55}
-{.$undef  HAS_XTYPES} {Xtra types in system: pByte, pLongint etc}
-{.$undef  HAS_UNSAFE} {UNSAFE warnings}
-{.$undef  APPCONS}    {Needs "Apptype console" for console application}
-{.$undef  FPC_ProcVar} {FPC handling of @ and proc variables}
+{.$undef BIT16}       {16 Bit code, Pascal / D1}
+{.$undef BIT32}       {32 Bit code}
+{.$undef DELPHI}      {Delphi2+ and BCB++}
+{.$undef G_OPT}       {G+ option support}
+{.$undef D4PLUS}      {Delphi 4 or higher}
+{.$undef BASM16}      {16 Bit BASM}
+{.$undef LoadArgs}    {Register params}
+{.$undef WINCRT}      {Use WinCRT for console}
+{.$undef RESULT}      {Result pseudo variable}
+{.$undef StrictLong}  {Warning for longint const with MS bit}
+{.$undef HAS_INT64}   { int64 integer type available}
+{.$undef HAS_UINT64}  {uint64 integer type available}
+{.$undef HAS_MSG}     {Has message directive}
+{.$undef HAS_INLINE}  {Has inline procs/funcs (D9)}
+{.$undef ABSTRACT}    {Has abstract methods}
+{.$undef DEFAULT}     {Support default parameters}
+{.$undef VER5X}       {TP5 or TP55}
+{.$undef HAS_XTYPES}  {Xtra types in system: pByte, pLongint etc}
+{.$undef HAS_UNSAFE}  {UNSAFE warnings}
+{.$undef APPCONS}     {Needs "Apptype console" for console application}
+{.$undef FPC_ProcVar} {FPC handling of @ and proc variables}
 
 {$define CONST}       {const in proc declaration}
 {$define Q_OPT}       {Q- option support}
 {$define X_OPT}       {X+ option support}
 {$define N_OPT}       {N+ option support}
+{$define V7PLUS}      {TP7 or higher}
 
 
 {$ifdef VER10}        {TPW 1.0??}
@@ -102,6 +107,7 @@
   {$define WINCRT}
   {$undef  CONST}
   {$undef  Q_OPT}
+  {$undef  V7PLUS}
 {$endif}
 
 {$ifdef VER15}        {TPW 1.5}
@@ -111,6 +117,7 @@
   {$define G_OPT}
   {$undef  CONST}
   {$undef  Q_OPT}
+  {$undef  V7PLUS}
 {$endif}
 
 {$ifdef VER50 }
@@ -119,6 +126,7 @@
   {$undef  CONST}
   {$undef  Q_OPT}
   {$undef  X_OPT}
+  {$undef  V7PLUS}
 {$endif}
 
 {$ifdef VER55 }
@@ -127,6 +135,7 @@
   {$undef  CONST}
   {$undef  Q_OPT}
   {$undef  X_OPT}
+  {$undef  V7PLUS}
 {$endif}
 
 {$ifdef VER60 }
@@ -135,6 +144,7 @@
   {$undef  Q_OPT}
   {$define G_OPT}
   {$define BASM16}
+  {$undef  V7PLUS}
 {$endif}
 
 {$ifdef VER70 }
@@ -193,6 +203,7 @@
   {$define DELPHI}  {D7}
   {$define D4PLUS}
   {$define HAS_UNSAFE}
+  {$define HAS_UINT64}
 {$endif}
 
 {$ifdef VER170}
@@ -200,13 +211,15 @@
   {$define D4PLUS}
   {$define HAS_INLINE}
   {$define HAS_UNSAFE}
+  {$define HAS_UINT64}
 {$endif}
 
 {$ifdef VER180}
-  {$define DELPHI}  {D10}
+  {$define DELPHI}  {D10, D11 ifdef VER185}
   {$define D4PLUS}
   {$define HAS_INLINE}
   {$define HAS_UNSAFE}
+  {$define HAS_UINT64}
 {$endif}
 
 {$ifdef CONDITIONALEXPRESSIONS}  {D6+}
@@ -262,6 +275,9 @@
     {$ifdef FPC_DELPHI}
       {$define DEFAULT}
     {$endif}
+    {$ifdef VER2_2}
+      {$define HAS_UINT64}
+    {$endif}
   {$endif}
   {Note: Mode detection does not work for -Sxxx and version < 2.0.2}
   {$ifdef FPC_OBJFPC}
@@ -338,6 +354,7 @@
 {$endif}
 
 {$ifdef Q_OPT}
+  {Most Crypto and CRC/Hash units need Q-, define Q+ locally if needed}
   {$Q-}
 {$endif}
 
@@ -365,6 +382,12 @@
 {$ifopt Q+} {$define OverflowChecks_on}  {$endif}
 {$endif}
 
+{-- Note that X option is GLOBAL --}
+{$ifdef X_OPT}
+{$ifopt X+} {$define ExtendedSyntax_on}  {$endif}
+{$ifopt X-} {$undef  RESULT}             {$endif}
+{$endif}
+
 {$ifdef CONDITIONALEXPRESSIONS}
   {$warn SYMBOL_PLATFORM OFF}
   {$warn SYMBOL_DEPRECATED OFF}
@@ -388,7 +411,10 @@
 {$endif}
 {
   $Log: std.inc,v $
-  Revision 1.2  2008/01/01 20:05:49  mw
+  Revision 1.3  2008/11/02 10:56:08  mw
+  MW: - Neues Upstream-Release der Crypto-Lib
+
+  Revision 1.2  2008-01-01 20:05:49  mw
   MW: - Neues Upstream-Release der Cryptolib eingebaut.
 
   Revision 1.1  2007-07-15 09:17:55  mw




Mehr Informationen über die CVS-List Mailingliste