| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636 |
- {******************************************************************************}
- {* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
- {******************************************************************************}
- {* Main component definitions *************************************************}
- {******************************************************************************}
- {* Copyright (c) 1999-2003 David Barton *}
- {* Permission is hereby granted, free of charge, to any person obtaining a *}
- {* copy of this software and associated documentation files (the "Software"), *}
- {* to deal in the Software without restriction, including without limitation *}
- {* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
- {* and/or sell copies of the Software, and to permit persons to whom the *}
- {* Software is furnished to do so, subject to the following conditions: *}
- {* *}
- {* The above copyright notice and this permission notice shall be included in *}
- {* all copies or substantial portions of the Software. *}
- {* *}
- {* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
- {* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
- {* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
- {* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
- {* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
- {* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
- {* DEALINGS IN THE SOFTWARE. *}
- {******************************************************************************}
- unit DCPcrypt2;
- interface
- uses
- Classes, Sysutils, DCPconst, DCPbase64;
- //{$DEFINE DCP1COMPAT} { DCPcrypt v1.31 compatiblity mode - see documentation }
- {******************************************************************************}
- { A few predefined types to help out }
- type
- Pbyte= ^byte;
- Pword= ^word;
- Pdword= ^dword;
- Pint64= ^int64;
- dword= longword;
- Pwordarray= ^Twordarray;
- Twordarray= array[0..19383] of word;
- Pdwordarray= ^Tdwordarray;
- Tdwordarray= array[0..8191] of dword;
- {******************************************************************************}
- { The base class from which all hash algorithms are to be derived }
- type
- EDCP_hash= class(Exception);
- TDCP_hash= class(TComponent)
- protected
- fInitialized: boolean; { Whether or not the algorithm has been initialized }
- procedure DeadInt(Value: integer); { Knudge to display vars in the object inspector }
- procedure DeadStr(Value: string); { Knudge to display vars in the object inspector }
-
- private
- function _GetId: integer;
- function _GetAlgorithm: string;
- function _GetHashSize: integer;
- public
- property Initialized: boolean
- read fInitialized;
- class function GetId: integer; virtual;
- { Get the algorithm id }
- class function GetAlgorithm: string; virtual;
- { Get the algorithm name }
- class function GetHashSize: integer; virtual;
- { Get the size of the digest produced - in bits }
- class function SelfTest: boolean; virtual;
- { Tests the implementation with several test vectors }
- procedure Init; virtual;
- { Initialize the hash algorithm }
- procedure Final(var Digest); virtual;
- { Create the final digest and clear the stored information.
- The size of the Digest var must be at least equal to the hash size }
- procedure Burn; virtual;
- { Clear any stored information with out creating the final digest }
- procedure Update(const Buffer; Size: longword); virtual;
- { Update the hash buffer with Size bytes of data from Buffer }
- procedure UpdateStream(Stream: TStream; Size: longword);
- { Update the hash buffer with Size bytes of data from the stream }
- procedure UpdateStr(const Str: AnsiString);
- { Update the hash buffer with the string }
- destructor Destroy; override;
- published
- property Id: integer
- read _GetId write DeadInt;
- property Algorithm: string
- read _GetAlgorithm write DeadStr;
- property HashSize: integer
- read _GetHashSize write DeadInt;
- end;
- TDCP_hashclass= class of TDCP_hash;
- {******************************************************************************}
- { The base class from which all encryption components will be derived. }
- { Stream ciphers will be derived directly from this class where as }
- { Block ciphers will have a further foundation class TDCP_blockcipher. }
- type
- EDCP_cipher= class(Exception);
- TDCP_cipher= class(TComponent)
- protected
- fInitialized: boolean; { Whether or not the key setup has been done yet }
- procedure DeadInt(Value: integer); { Knudge to display vars in the object inspector }
- procedure DeadStr(Value: string); { Knudge to display vars in the object inspector }
- private
- function _GetId: integer;
- function _GetAlgorithm: string;
- function _GetMaxKeySize: integer;
- public
- property Initialized: boolean
- read fInitialized;
- class function GetId: integer; virtual;
- { Get the algorithm id }
- class function GetAlgorithm: string; virtual;
- { Get the algorithm name }
- class function GetMaxKeySize: integer; virtual;
- { Get the maximum key size (in bits) }
- class function SelfTest: boolean; virtual;
- { Tests the implementation with several test vectors }
- procedure Init(const Key; Size: longword; InitVector: pointer); virtual;
- { Do key setup based on the data in Key, size is in bits }
- procedure InitStr(const Key: AnsiString; HashType: TDCP_hashclass);
- { Do key setup based on a hash of the key string }
- procedure Burn; virtual;
- { Clear all stored key information }
- procedure Reset; virtual;
- { Reset any stored chaining information }
- procedure Encrypt(const Indata; var Outdata; Size: longword); virtual;
- { Encrypt size bytes of data and place in Outdata }
- procedure Decrypt(const Indata; var Outdata; Size: longword); virtual;
- { Decrypt size bytes of data and place in Outdata }
- function EncryptStream(InStream, OutStream: TStream; Size: longword): longword;
- { Encrypt size bytes of data from InStream and place in OutStream }
- function DecryptStream(InStream, OutStream: TStream; Size: longword): longword;
- { Decrypt size bytes of data from InStream and place in OutStream }
- function EncryptString(const Str: AnsiString): AnsiString; virtual;
- { Encrypt a string and return Base64 encoded }
- function DecryptString(const Str: AnsiString): AnsiString; virtual;
- { Decrypt a Base64 encoded string }
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- published
- property Id: integer
- read _GetId write DeadInt;
- property Algorithm: string
- read _GetAlgorithm write DeadStr;
- property MaxKeySize: integer
- read _GetMaxKeySize write DeadInt;
- end;
- TDCP_cipherclass= class of TDCP_cipher;
- {******************************************************************************}
- { The base class from which all block ciphers are to be derived, this }
- { extra class takes care of the different block encryption modes. }
- type
- TDCP_ciphermode= (cmCBC, cmCFB8bit, cmCFBblock, cmOFB, cmCTR); // cmCFB8bit is equal to DCPcrypt v1.xx's CFB mode
- EDCP_blockcipher= class(EDCP_cipher);
- TDCP_blockcipher= class(TDCP_cipher)
- protected
- fCipherMode: TDCP_ciphermode; { The cipher mode the encrypt method uses }
- procedure InitKey(const Key; Size: longword); virtual;
- private
- function _GetBlockSize: integer;
- public
- class function GetBlockSize: integer; virtual;
- { Get the block size of the cipher (in bits) }
- procedure SetIV(const Value); virtual;
- { Sets the IV to Value and performs a reset }
- procedure GetIV(var Value); virtual;
- { Returns the current chaining information, not the actual IV }
- procedure Encrypt(const Indata; var Outdata; Size: longword); override;
- { Encrypt size bytes of data and place in Outdata using CipherMode }
- procedure Decrypt(const Indata; var Outdata; Size: longword); override;
- { Decrypt size bytes of data and place in Outdata using CipherMode }
- function EncryptString(const Str: AnsiString): AnsiString; override;
- { Encrypt a string and return Base64 encoded }
- function DecryptString(const Str: AnsiString): AnsiString; override;
- { Decrypt a Base64 encoded string }
- procedure EncryptECB(const Indata; var Outdata); virtual;
- { Encrypt a block of data using the ECB method of encryption }
- procedure DecryptECB(const Indata; var Outdata); virtual;
- { Decrypt a block of data using the ECB method of decryption }
- procedure EncryptCBC(const Indata; var Outdata; Size: longword); virtual;
- { Encrypt size bytes of data using the CBC method of encryption }
- procedure DecryptCBC(const Indata; var Outdata; Size: longword); virtual;
- { Decrypt size bytes of data using the CBC method of decryption }
- procedure EncryptCFB8bit(const Indata; var Outdata; Size: longword); virtual;
- { Encrypt size bytes of data using the CFB (8 bit) method of encryption }
- procedure DecryptCFB8bit(const Indata; var Outdata; Size: longword); virtual;
- { Decrypt size bytes of data using the CFB (8 bit) method of decryption }
- procedure EncryptCFBblock(const Indata; var Outdata; Size: longword); virtual;
- { Encrypt size bytes of data using the CFB (block) method of encryption }
- procedure DecryptCFBblock(const Indata; var Outdata; Size: longword); virtual;
- { Decrypt size bytes of data using the CFB (block) method of decryption }
- procedure EncryptOFB(const Indata; var Outdata; Size: longword); virtual;
- { Encrypt size bytes of data using the OFB method of encryption }
- procedure DecryptOFB(const Indata; var Outdata; Size: longword); virtual;
- { Decrypt size bytes of data using the OFB method of decryption }
- procedure EncryptCTR(const Indata; var Outdata; Size: longword); virtual;
- { Encrypt size bytes of data using the CTR method of encryption }
- procedure DecryptCTR(const Indata; var Outdata; Size: longword); virtual;
- { Decrypt size bytes of data using the CTR method of decryption }
- constructor Create(AOwner: TComponent); override;
- published
- property BlockSize: integer
- read _GetBlockSize write DeadInt;
- property CipherMode: TDCP_ciphermode
- read fCipherMode write fCipherMode default cmCBC;
- end;
- TDCP_blockcipherclass= class of TDCP_blockcipher;
- {******************************************************************************}
- { Helper functions }
- procedure XorBlock(var InData1, InData2; Size: longword);
- implementation
- {$Q-}{$R-}
- {** TDCP_hash *****************************************************************}
- procedure TDCP_hash.DeadInt(Value: integer);
- begin
- end;
- procedure TDCP_hash.DeadStr(Value: string);
- begin
- end;
- function TDCP_hash._GetId: integer;
- begin
- Result:= GetId;
- end;
- function TDCP_hash._GetAlgorithm: string;
- begin
- Result:= GetAlgorithm;
- end;
- function TDCP_hash._GetHashSize: integer;
- begin
- Result:= GetHashSize;
- end;
- class function TDCP_hash.GetId: integer;
- begin
- Result:= -1;
- end;
- class function TDCP_hash.GetAlgorithm: string;
- begin
- Result:= '';
- end;
- class function TDCP_hash.GetHashSize: integer;
- begin
- Result:= -1;
- end;
- class function TDCP_hash.SelfTest: boolean;
- begin
- Result:= false;
- end;
- procedure TDCP_hash.Init;
- begin
- end;
- procedure TDCP_hash.Final(var Digest);
- begin
- end;
- procedure TDCP_hash.Burn;
- begin
- end;
- procedure TDCP_hash.Update(const Buffer; Size: longword);
- begin
- end;
- procedure TDCP_hash.UpdateStream(Stream: TStream; Size: longword);
- var
- Buffer: array[0..8191] of byte;
- i, read: integer;
- begin
- for i:= 1 to (Size div Sizeof(Buffer)) do
- begin
- read:= Stream.Read(Buffer,Sizeof(Buffer));
- Update(Buffer,read);
- end;
- if (Size mod Sizeof(Buffer))<> 0 then
- begin
- read:= Stream.Read(Buffer,Size mod Sizeof(Buffer));
- Update(Buffer,read);
- end;
- end;
- procedure TDCP_hash.UpdateStr(const Str: AnsiString);
- begin
- Update(Str[1],Length(Str));
- end;
- destructor TDCP_hash.Destroy;
- begin
- if fInitialized then
- Burn;
- inherited Destroy;
- end;
- {** TDCP_cipher ***************************************************************}
- procedure TDCP_cipher.DeadInt(Value: integer);
- begin
- end;
- procedure TDCP_cipher.DeadStr(Value: string);
- begin
- end;
- function TDCP_cipher._GetId: integer;
- begin
- Result:= GetId;
- end;
- function TDCP_cipher._GetAlgorithm: string;
- begin
- Result:= GetAlgorithm;
- end;
- function TDCP_cipher._GetMaxKeySize: integer;
- begin
- Result:= GetMaxKeySize;
- end;
- class function TDCP_cipher.GetId: integer;
- begin
- Result:= -1;
- end;
- class function TDCP_cipher.GetAlgorithm: string;
- begin
- Result:= '';
- end;
- class function TDCP_cipher.GetMaxKeySize: integer;
- begin
- Result:= -1;
- end;
- class function TDCP_cipher.SelfTest: boolean;
- begin
- Result:= false;
- end;
- procedure TDCP_cipher.Init(const Key; Size: longword; InitVector: pointer);
- begin
- if fInitialized then
- Burn;
- if (Size <= 0) or ((Size and 3)<> 0) or (Size> longword(GetMaxKeySize)) then
- raise EDCP_cipher.Create('Invalid key size')
- else
- fInitialized:= true;
- end;
- procedure TDCP_cipher.InitStr(const Key: AnsiString; HashType: TDCP_hashclass);
- var
- Hash: TDCP_hash;
- Digest: pointer;
- begin
- if fInitialized then
- Burn;
- try
- GetMem(Digest,HashType.GetHashSize div 8);
- Hash:= HashType.Create(Self);
- Hash.Init;
- Hash.UpdateStr(Key);
- Hash.Final(Digest^);
- Hash.Free;
- if MaxKeySize< HashType.GetHashSize then
- Init(Digest^,MaxKeySize,nil)
- else
- Init(Digest^,HashType.GetHashSize,nil);
- FillChar(Digest^,HashType.GetHashSize div 8,$FF);
- FreeMem(Digest);
- except
- raise EDCP_cipher.Create('Unable to allocate sufficient memory for hash digest');
- end;
- end;
- procedure TDCP_cipher.Burn;
- begin
- fInitialized:= false;
- end;
- procedure TDCP_cipher.Reset;
- begin
- end;
- procedure TDCP_cipher.Encrypt(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_cipher.Decrypt(const Indata; var Outdata; Size: longword);
- begin
- end;
- function TDCP_cipher.EncryptStream(InStream, OutStream: TStream; Size: longword): longword;
- var
- Buffer: array[0..8191] of byte;
- i, Read: longword;
- begin
- Result:= 0;
- for i:= 1 to (Size div Sizeof(Buffer)) do
- begin
- Read:= InStream.Read(Buffer,Sizeof(Buffer));
- Inc(Result,Read);
- Encrypt(Buffer,Buffer,Read);
- OutStream.Write(Buffer,Read);
- end;
- if (Size mod Sizeof(Buffer))<> 0 then
- begin
- Read:= InStream.Read(Buffer,Size mod Sizeof(Buffer));
- Inc(Result,Read);
- Encrypt(Buffer,Buffer,Read);
- OutStream.Write(Buffer,Read);
- end;
- end;
- function TDCP_cipher.DecryptStream(InStream, OutStream: TStream; Size: longword): longword;
- var
- Buffer: array[0..8191] of byte;
- i, Read: longword;
- begin
- Result:= 0;
- for i:= 1 to (Size div Sizeof(Buffer)) do
- begin
- Read:= InStream.Read(Buffer,Sizeof(Buffer));
- Inc(Result,Read);
- Decrypt(Buffer,Buffer,Read);
- OutStream.Write(Buffer,Read);
- end;
- if (Size mod Sizeof(Buffer))<> 0 then
- begin
- Read:= InStream.Read(Buffer,Size mod Sizeof(Buffer));
- Inc(Result,Read);
- Decrypt(Buffer,Buffer,Read);
- OutStream.Write(Buffer,Read);
- end;
- end;
- function TDCP_cipher.EncryptString(const Str: AnsiString): AnsiString;
- begin
- SetLength(Result,Length(Str));
- Encrypt(Str[1],Result[1],Length(Str));
- Result:= Base64EncodeStr(Result);
- end;
- function TDCP_cipher.DecryptString(const Str: AnsiString): AnsiString;
- begin
- Result:= Base64DecodeStr(Str);
- Decrypt(Result[1],Result[1],Length(Result));
- end;
- constructor TDCP_cipher.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- Burn;
- end;
- destructor TDCP_cipher.Destroy;
- begin
- if fInitialized then
- Burn;
- inherited Destroy;
- end;
- {** TDCP_blockcipher **********************************************************}
- procedure TDCP_blockcipher.InitKey(const Key; Size: longword);
- begin
- end;
- function TDCP_blockcipher._GetBlockSize: integer;
- begin
- Result:= GetBlockSize;
- end;
- class function TDCP_blockcipher.GetBlockSize: integer;
- begin
- Result:= -1;
- end;
- procedure TDCP_blockcipher.SetIV(const Value);
- begin
- end;
- procedure TDCP_blockcipher.GetIV(var Value);
- begin
- end;
- procedure TDCP_blockcipher.Encrypt(const Indata; var Outdata; Size: longword);
- begin
- case fCipherMode of
- cmCBC: EncryptCBC(Indata,Outdata,Size);
- cmCFB8bit: EncryptCFB8bit(Indata,Outdata,Size);
- cmCFBblock: EncryptCFBblock(Indata,Outdata,Size);
- cmOFB: EncryptOFB(Indata,Outdata,Size);
- cmCTR: EncryptCTR(Indata,Outdata,Size);
- end;
- end;
- function TDCP_blockcipher.EncryptString(const Str: AnsiString): AnsiString;
- begin
- SetLength(Result,Length(Str));
- EncryptCFB8bit(Str[1],Result[1],Length(Str));
- Result:= Base64EncodeStr(Result);
- end;
- function TDCP_blockcipher.DecryptString(const Str: AnsiString): AnsiString;
- begin
- Result:= Base64DecodeStr(Str);
- DecryptCFB8bit(Result[1],Result[1],Length(Result));
- end;
- procedure TDCP_blockcipher.Decrypt(const Indata; var Outdata; Size: longword);
- begin
- case fCipherMode of
- cmCBC: DecryptCBC(Indata,Outdata,Size);
- cmCFB8bit: DecryptCFB8bit(Indata,Outdata,Size);
- cmCFBblock: DecryptCFBblock(Indata,Outdata,Size);
- cmOFB: DecryptOFB(Indata,Outdata,Size);
- cmCTR: DecryptCTR(Indata,Outdata,Size);
- end;
- end;
- procedure TDCP_blockcipher.EncryptECB(const Indata; var Outdata);
- begin
- end;
- procedure TDCP_blockcipher.DecryptECB(const Indata; var Outdata);
- begin
- end;
- procedure TDCP_blockcipher.EncryptCBC(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_blockcipher.DecryptCBC(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_blockcipher.EncryptCFB8bit(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_blockcipher.DecryptCFB8bit(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_blockcipher.EncryptCFBblock(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_blockcipher.DecryptCFBblock(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_blockcipher.EncryptOFB(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_blockcipher.DecryptOFB(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_blockcipher.EncryptCTR(const Indata; var Outdata; Size: longword);
- begin
- end;
- procedure TDCP_blockcipher.DecryptCTR(const Indata; var Outdata; Size: longword);
- begin
- end;
- constructor TDCP_blockcipher.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- fCipherMode:= cmCBC;
- end;
- {** Helpher functions *********************************************************}
- procedure XorBlock(var InData1, InData2; Size: longword);
- var
- i: longword;
- begin
- for i:= 1 to Size do
- Pbyte(longword(@InData1)+i-1)^:= Pbyte(longword(@InData1)+i-1)^ xor Pbyte(longword(@InData2)+i-1)^;
- end;
- end.
|