📄 lbcipher.pas
字号:
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is TurboPower LockBox
*
* The Initial Developer of the Original Code is
* TurboPower Software
*
* Portions created by the Initial Developer are Copyright (C) 1997-2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** *)
{*********************************************************}
{* LBCIPHER.PAS 2.07 *}
{* Copyright (c) 2002 TurboPower Software Co *}
{* All rights reserved. *}
{*********************************************************}
{$I LockBox.inc}
unit LbCipher;
{-private key encryption/decryption primitives}
interface
uses
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
{$IFDEF UsingCLX}
Types,
{$ENDIF}
Classes;
const
{ largest structure that can be created }
MaxStructSize = 1024 * 2000000; {2G}
{ TLbBase - used to force this unit to be added to uses clause }
type
TLBBase = class(TComponent)
end;
{$IFDEF LINUX}
pDword = ^dword;
{$ENDIF}
{ general structures }
type
pLongIntArray = ^TLongIntArray;
TLongIntArray = array [0..MaxStructSize div SizeOf(LongInt) - 1] of LongInt;
TLongIntRec = packed record
case Byte of
1: (Lo: Word;
Hi: Word);
2: (LoLo: Byte;
LoHi: Byte;
HiLo: Byte;
HiHi: Byte);
end;
TInt64 = packed record
case Byte of
0: (Lo: LongInt;
Hi: LongInt);
1: (LoLo: Word;
LoHi: Word;
HiLo: Word;
HiHi: Word);
2: (LoLoLo: Byte;
LoLoHi: Byte;
LoHiLo: Byte;
LoHiHi: Byte;
HiLoLo: Byte;
HiLoHi: Byte;
HiHiLo: Byte;
HiHiHi: Byte);
end;
TRDLVector = record
case Byte of
0 : (dw : DWord);
1 : (bt : array[0..3] of Byte);
end;
{ encryption key types }
type
PKey64 = ^TKey64; {!!.03}
TKey64 = array [0..7] of Byte;
PKey128 = ^TKey128; {!!.03}
TKey128 = array [0..15] of Byte;
PKey192 = ^TKey192; {!!.03}
TKey192 = array [0..23] of Byte;
PKey256 = ^TKey256; {!!.03}
TKey256 = array [0..31] of Byte;
{ encryption block types }
type
PLBCBlock = ^TLBCBlock;
TLBCBlock = array[0..3] of LongInt; { LockBox Cipher }
TDESBlock = array[0..7] of Byte; { DES }
TLQCBlock = array[0..1] of LongInt; { LockBox Quick Cipher }
TBFBlock = array[0..1] of LongInt; { BlowFish }
TRDLBlock = array[0..15] of Byte; { Rijndael }
{ context type constants }
const
BFRounds = 16; { 16 blowfish rounds }
MaxRDLRounds = 14; { 14 Rijndael rounds }
{ block cipher context types }
type
{ Blowfish }
TBFContext = packed record
PBox : array[0..(BFRounds+1)] of LongInt;
SBox : array[0..3, 0..255] of LongInt;
end;
{ DES }
TDESContext = packed record
TransformedKey : array [0..31] of LongInt;
Encrypt : Boolean;
end;
{ 3 DES }
TTripleDESContext = array [0..1] of TDESContext;
TTripleDESContext3Key = array [0..2] of TDESContext; {!!.01}
{ LockBox Cipher }
TLBCContext = packed record
Encrypt : Boolean;
Dummy : array[0..2] of Byte; {filler}
Rounds : LongInt;
case Byte of
0: (SubKeys64 : array [0..15] of TKey64);
1: (SubKeysInts : array [0..3, 0..7] of LongInt);
end;
{ Rijndael }
TRDLContext = packed record
Encrypt : Boolean;
Dummy : array[0..2] of Byte; {filler}
Rounds : DWord;
case Byte of
0 : (W : array[0..(MaxRDLRounds * 4)] of TRDLVector);
1 : (Rk : array[0..MaxRDLRounds] of TRDLBlock);
end;
{ stream cipher context types }
type
{ LockBox stream cipher }
TLSCContext = packed record
Index : LongInt;
Accumulator : LongInt;
SBox : array [0..255] of Byte;
end;
{ random number stream ciphers }
TRNG32Context = array [0..3] of Byte;
TRNG64Context = array [0..7] of Byte;
{ message digest blocks }
type
TMD5Digest = array [0..15] of Byte; { 128 bits - MD5 }
TSHA1Digest = array [0..19] of Byte; { 160 bits - SHA-1 }
{ message digest context types }
type
TLMDContext = array [0..279] of Byte; { LockBox message digest }
TMD5Context = array [0..87] of Byte; { MD5 }
TSHA1Context = record { SHA-1 }
sdHi : DWord;
sdLo : DWord;
sdIndex : DWord;
sdHash : array [0..4] of DWord;
sdBuf : array [0..63] of Byte;
end;
{ Blowfish Cipher }
procedure InitEncryptBF(Key : TKey128;
var Context : TBFContext);
procedure EncryptBF(const Context : TBFContext;
var Block : TBFBlock; Encrypt : Boolean);
procedure EncryptBFCBC(const Context : TBFContext;
const Prev : TBFBlock; var Block : TBFBlock; Encrypt : Boolean);
{ DES Cipher }
procedure InitEncryptDES(const Key : TKey64;
var Context : TDESContext; Encrypt : Boolean);
procedure EncryptDES(const Context : TDESContext;
var Block : TDESBlock);
procedure EncryptDESCBC(const Context : TDESContext;
const Prev : TDESBlock; var Block : TDESBlock);
{ Triple DES Cipher }
procedure InitEncryptTripleDES(const Key : TKey128;
var Context : TTripleDESContext; Encrypt : Boolean);
procedure EncryptTripleDES(const Context : TTripleDESContext;
var Block : TDESBlock);
procedure EncryptTripleDESCBC(const Context : TTripleDESContext;
const Prev : TDESBlock; var Block : TDESBlock);
{!!.01}
{ Triple DES Cipher 3 Key }
procedure InitEncryptTripleDES3Key(const Key1, Key2, Key3 : TKey64;
var Context : TTripleDESContext3Key; Encrypt : Boolean);
procedure EncryptTripleDES3Key(const Context : TTripleDESContext3Key;
var Block : TDESBlock);
procedure EncryptTripleDESCBC3Key(const Context : TTripleDESContext3Key;
const Prev : TDESBlock; var Block : TDESBlock);
{!!.01}
{ LockBox Cipher }
procedure InitEncryptLBC(const Key : TKey128;
var Context : TLBCContext; Rounds : LongInt; Encrypt : Boolean);
procedure EncryptLBC(const Context : TLBCContext;
var Block : TLBCBlock);
procedure EncryptLBCCBC(const Context : TLBCContext;
const Prev : TLBCBlock; var Block : TLBCBlock);
{ LockBox Quick Cipher }
procedure EncryptLQC(const Key : TKey128;
var Block : TLQCBlock; Encrypt : Boolean);
procedure EncryptLQCCBC(const Key : TKey128;
const Prev : TLQCBlock; var Block : TLQCBlock; Encrypt : Boolean);
{ LockBox Stream Cipher }
procedure InitEncryptLSC(const Key; KeySize : Integer;
var Context : TLSCContext);
procedure EncryptLSC(var Context : TLSCContext;
var Buf; BufSize : LongInt);
{ Random Number Cipher }
procedure InitEncryptRNG64(KeyHi, KeyLo : LongInt;
var Context : TRNG64Context);
procedure EncryptRNG32(var Context : TRNG32Context;
var Buf; BufSize : LongInt);
procedure EncryptRNG64(var Context : TRNG64Context;
var Buf; BufSize : LongInt);
procedure InitEncryptRNG32(Key : LongInt;
var Context : TRNG32Context);
{ Rijndael Cipher }
procedure InitEncryptRDL(const Key; KeySize : Longint;
var Context : TRDLContext; Encrypt : Boolean);
procedure EncryptRDL(const Context : TRDLContext;
var Block : TRDLBlock);
procedure EncryptRDLCBC(const Context : TRDLContext;
const Prev : TRDLBlock; var Block : TRDLBlock);
{ MD5 message digest }
procedure InitMD5(var Context : TMD5Context);
procedure HashMD5(var Digest : TMD5Digest;
const Buf; BufSize : LongInt);
procedure FinalizeMD5(var Context : TMD5Context;
var Digest : TMD5Digest);
procedure UpdateMD5(var Context : TMD5Context;
const Buf; BufSize : LongInt);
{ LockBox message digest }
procedure InitLMD(var Context : TLMDContext);
procedure HashLMD(var Digest; DigestSize : LongInt;
const Buf; BufSize : LongInt);
procedure FinalizeLMD(var Context : TLMDContext;
var Digest; DigestSize : LongInt);
procedure UpdateLMD(var Context : TLMDContext;
const Buf; BufSize : LongInt);
{ SHA-1 message digest }
procedure InitSHA1(var Context: TSHA1Context);
procedure HashSHA1(var Digest : TSHA1Digest;
const Buf; BufSize : Longint);
procedure UpdateSHA1(var Context : TSHA1Context;
const Buf; BufSize: Longint);
procedure FinalizeSHA1(var Context: TSHA1Context;
var Digest : TSHA1Digest);
{ Miscellaneous hash algorithms }
procedure HashELF(var Digest : LongInt;
const Buf; BufSize : LongInt);
procedure HashMix128(var Digest : LongInt;
const Buf; BufSize : LongInt);
{ String hashing }
procedure StringHashELF(var Digest : LongInt;
const Str : string);
procedure StringHashLMD(var Digest; DigestSize : LongInt;
const Str : string);
procedure StringHashMD5(var Digest : TMD5Digest;
const Str : string);
procedure StringHashMix128(var Digest : LongInt;
const Str : string);
procedure StringHashSHA1(var Digest : TSHA1Digest;
const Str : string);
{ Key generation }
procedure GenerateLMDKey(var Key; KeySize : Integer;
const Str : string);
procedure GenerateMD5Key(var Key : TKey128;
const Str : string);
procedure GenerateRandomKey(var Key; KeySize : Integer);
{ Misc public utilities }
function Ran01(var Seed : LongInt) : LongInt;
function Ran02(var Seed : LongInt) : LongInt;
function Ran03(var Seed : LongInt) : LongInt;
function Random32Byte(var Seed : LongInt) : Byte;
function Random64Byte(var Seed : TInt64) : Byte;
procedure ShrinkDESKey(var Key : TKey64);
procedure XorMem(var Mem1; const Mem2; Count : Cardinal);
function RolX(I, C : DWord) : DWord; register;
implementation
uses
LbUtils, SysUtils;
{first 2048 bits of Pi in hexadecimal, low to high, without the leading "3"}
const
Pi2048: array [0..255] of Byte = (
$24, $3F, $6A, $88, $85, $A3, $08, $D3, $13, $19, $8A, $2E, $03, $70, $73, $44,
$A4, $09, $38, $22, $29, $9F, $31, $D0, $08, $2E, $FA, $98, $EC, $4E, $6C, $89,
$45, $28, $21, $E6, $38, $D0, $13, $77, $BE, $54, $66, $CF, $34, $E9, $0C, $6C,
$C0, $AC, $29, $B7, $C9, $7C, $50, $DD, $3F, $84, $D5, $B5, $B5, $47, $09, $17,
$92, $16, $D5, $D9, $89, $79, $FB, $1B, $D1, $31, $0B, $A6, $98, $DF, $B5, $AC,
$2F, $FD, $72, $DB, $D0, $1A, $DF, $B7, $B8, $E1, $AF, $ED, $6A, $26, $7E, $96,
$BA, $7C, $90, $45, $F1, $2C, $7F, $99, $24, $A1, $99, $47, $B3, $91, $6C, $F7,
$08, $01, $F2, $E2, $85, $8E, $FC, $16, $63, $69, $20, $D8, $71, $57, $4E, $69,
$A4, $58, $FE, $A3, $F4, $93, $3D, $7E, $0D, $95, $74, $8F, $72, $8E, $B6, $58,
$71, $8B, $CD, $58, $82, $15, $4A, $EE, $7B, $54, $A4, $1D, $C2, $5A, $59, $B5,
$9C, $30, $D5, $39, $2A, $F2, $60, $13, $C5, $D1, $B0, $23, $28, $60, $85, $F0,
$CA, $41, $79, $18, $B8, $DB, $38, $EF, $8E, $79, $DC, $B0, $60, $3A, $18, $0E,
$6C, $9E, $0E, $8B, $B0, $1E, $8A, $3E, $D7, $15, $77, $C1, $BD, $31, $4B, $27,
$78, $AF, $2F, $DA, $55, $60, $5C, $60, $E6, $55, $25, $F3, $AA, $55, $AB, $94,
$57, $48, $98, $62, $63, $E8, $14, $40, $55, $CA, $39, $6A, $2A, $AB, $10, $B6,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -