FPSoreadsheet: Remove dependence on DCPCrypt from the crypto package (use Wolfgang-Ehrhardt units instead and add the needed ones to the package).

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8907 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2023-08-02 10:37:14 +00:00
parent c0c19b0748
commit f0e01409ec
39 changed files with 7164 additions and 72 deletions

View File

@ -8,13 +8,13 @@
<Version Value="11"/>
<PathDelim Value="\"/>
<SearchPaths>
<OtherUnitFiles Value="source\crypto"/>
<OtherUnitFiles Value="source\crypto;source\crypto\3rdParty\aes;source\crypto\3rdParty\fcl-hash"/>
<UnitOutputDirectory Value="source\lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
</CompilerOptions>
<Description Value="Encryption / decryption support for FPSpreadsheet"/>
<Version Major="1" Minor="14"/>
<Files Count="2">
<Files Count="3">
<Item1>
<Filename Value="source\crypto\xlsxdecrypter.pas"/>
<UnitName Value="xlsxdecrypter"/>
@ -23,20 +23,21 @@
<Filename Value="source\crypto\xlsxooxml_crypto.pas"/>
<UnitName Value="xlsxooxml_crypto"/>
</Item2>
<Item3>
<Filename Value="source\crypto\fpscryptoproc.pas"/>
<UnitName Value="fpscryptoproc"/>
</Item3>
</Files>
<CompatibilityMode Value="True"/>
<RequiredPkgs Count="3">
<RequiredPkgs Count="2">
<Item1>
<PackageName Value="dcpcrypt"/>
</Item1>
<Item2>
<PackageName Value="laz_fpspreadsheet"/>
<MaxVersion Major="1" Minor="11"/>
<MinVersion Major="1" Minor="12" Valid="True"/>
</Item2>
<Item3>
</Item1>
<Item2>
<PackageName Value="FCL"/>
</Item3>
</Item2>
</RequiredPkgs>
<UsageOptions>
<UnitPath Value="$(PkgOutDir)"/>

View File

@ -87,7 +87,7 @@ type
EncryptionData_ChecksumType: String;
// Encrypting the data with the pwd hash ("key")
AlgorithmName: String;
InitializationVector: String;
InitializationVector: RawByteString;
IterationCount: Integer;
// Start key generation
StartKeyGenerationName: String;
@ -95,7 +95,7 @@ type
// Key derivation (encrypting and salting the start key)
KeyDerivationName: String;
KeySize: Integer;
Salt: String;
Salt: RawByteString;
end;
{ TsSpreadOpenDocReader }
@ -204,6 +204,8 @@ type
function Decrypt(AStream: TStream; ADecryptionInfo: TsOpenDocManifestFileEntry;
APassword: RawByteString; ADestStream: TStream): Boolean; virtual;
function SupportsDecryption: Boolean; virtual;
function UnzipToStream(AStream: TStream; AZippedFile: String;
ADestStream: TStream): Boolean; virtual;
function UnzipToStream(AStream: TStream; AZippedFile: String;
APassword: RawByteString; ADestStream: TStream): Boolean;
public
@ -2916,7 +2918,7 @@ begin
// Read the META-INF/manifest.xml file to learn about encryption
XMLStream := CreateXMLStream;
try
if fpsXMLCommon.UnzipToStream(AStream, 'META-INF/manifest.xml', XMLStream) then
if UnzipToStream(AStream, 'META-INF/manifest.xml', XMLStream) then
begin
ReadXMLStream(Doc, XMLStream);
if Assigned(Doc) then
@ -5612,6 +5614,12 @@ begin
Result := false;
end;
function TsSpreadOpenDocReader.UnzipToStream(AStream: TStream;
AZippedFile: String; ADestStream: TStream): Boolean;
begin
Result := fpsXMLCommon.UnzipToStream(AStream, AZippedFile, ADestStream);
end;
function TsSpreadOpenDocReader.UnzipToStream(AStream: TStream; AZippedFile: String;
APassword: RawByteString; ADestStream: TStream): Boolean;
var
@ -5636,14 +5644,14 @@ begin
tmpStream := TMemoryStream.Create;
try
// Read the encrypted file from the input stream
Result := fpsXMLCommon.UnzipToStream(AStream, AZippedFile, tmpStream);
Result := UnzipToStream(AStream, AZippedFile, tmpStream);
// Decrypt the file into the destination stream
Result := Result and Decrypt(tmpStream, mfe, APassword, ADestStream);
finally
tmpStream.Free;
end;
end else
Result := fpsXMLCommon.UnzipToStream(AStream, AZippedFile, ADestStream);
Result := UnzipToStream(AStream, AZippedFile, ADestStream);
end;
{ TsSpreadOpenDocWriter }

View File

@ -0,0 +1,5 @@
This folder contains a selection of the cryptographic units by Wolfgang Ehrhardt
as needed by the decryption code in FPSpreadsheet.
All files, together with sample and test projects, can be found at
https://github.com/chadilukito/www.wolfgang-ehrhardt.de/tree/master.

View File

@ -0,0 +1,385 @@
unit AES_Base;
(*************************************************************************
DESCRIPTION : AES basic routines
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12/D17, FPC, VP
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
REFERENCES : [1] http://csrc.nist.gov/fips/fips-197.pdf
[2] rijndael-alg-fst.c V2.0/3.0: Rijmen et al Aug1999/Dec2000
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.23 16.08.03 we From AESCrypt
0.24 16.08.03 we new xor_block
0.25 18.09.03 we Static tables, GF routines moved to aes_decr
0.26 21.09.03 we routines as functions
0.27 27.09.03 we FPC/go32v2
0.28 05.10.03 we STD.INC, TP5-6
0.29 07.12.03 we BugFix: exit if invalid key length
0.30 27.12.03 we BASM16: xorblock
0.31 01.01.04 we RotWord inline via shl/shr, SubWord function
0.32 15.01.04 we Keysetup like [2]
0.33 15.01.04 we BIT16: Keysetup with byte arrays
0.34 06.03.04 we removed exit in 128 bit key setup
0.35 02.07.04 we {$ifdef DLL} stdcall; {$endif}
0.36 12.10.04 we key setup with pointers
0.37 29.11.04 we FastInit
0.38 30.11.04 we AES_XorBlock, AESBLKSIZE
0.39 24.12.04 we Helper types PWA4, PLong
0.40 24.12.04 we FastInit, AES_Get/SetFastInit
0.41 09.07.06 we Checked: D9-D10
0.42 25.12.12 we {$J+} if needed
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2002-2012 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{$i STD.INC}
interface
uses AES_Type;
{helper types}
type
TWA4 = packed array[0..3] of longint; {AES block as array of longint}
TBA4 = packed array[0..3] of byte; {AES "word" as array of byte }
TAWk = packed array[0..4*(AESMaxRounds+1)-1] of longint; {Key as array of longint}
PWA4 = ^TWA4;
PAWk = ^TAWk;
{-AES static tables}
const
SBox: array[byte] of byte =
($63, $7c, $77, $7b, $f2, $6b, $6f, $c5, $30, $01, $67, $2b, $fe, $d7, $ab, $76,
$ca, $82, $c9, $7d, $fa, $59, $47, $f0, $ad, $d4, $a2, $af, $9c, $a4, $72, $c0,
$b7, $fd, $93, $26, $36, $3f, $f7, $cc, $34, $a5, $e5, $f1, $71, $d8, $31, $15,
$04, $c7, $23, $c3, $18, $96, $05, $9a, $07, $12, $80, $e2, $eb, $27, $b2, $75,
$09, $83, $2c, $1a, $1b, $6e, $5a, $a0, $52, $3b, $d6, $b3, $29, $e3, $2f, $84,
$53, $d1, $00, $ed, $20, $fc, $b1, $5b, $6a, $cb, $be, $39, $4a, $4c, $58, $cf,
$d0, $ef, $aa, $fb, $43, $4d, $33, $85, $45, $f9, $02, $7f, $50, $3c, $9f, $a8,
$51, $a3, $40, $8f, $92, $9d, $38, $f5, $bc, $b6, $da, $21, $10, $ff, $f3, $d2,
$cd, $0c, $13, $ec, $5f, $97, $44, $17, $c4, $a7, $7e, $3d, $64, $5d, $19, $73,
$60, $81, $4f, $dc, $22, $2a, $90, $88, $46, $ee, $b8, $14, $de, $5e, $0b, $db,
$e0, $32, $3a, $0a, $49, $06, $24, $5c, $c2, $d3, $ac, $62, $91, $95, $e4, $79,
$e7, $c8, $37, $6d, $8d, $d5, $4e, $a9, $6c, $56, $f4, $ea, $65, $7a, $ae, $08,
$ba, $78, $25, $2e, $1c, $a6, $b4, $c6, $e8, $dd, $74, $1f, $4b, $bd, $8b, $8a,
$70, $3e, $b5, $66, $48, $03, $f6, $0e, $61, $35, $57, $b9, $86, $c1, $1d, $9e,
$e1, $f8, $98, $11, $69, $d9, $8e, $94, $9b, $1e, $87, $e9, $ce, $55, $28, $df,
$8c, $a1, $89, $0d, $bf, $e6, $42, $68, $41, $99, $2d, $0f, $b0, $54, $bb, $16);
{$ifdef CONST}
procedure AES_XorBlock(const B1, B2: TAESBlock; var B3: TAESBlock);
{-xor two blocks, result in third}
{$ifdef DLL} stdcall; {$endif}
function AES_Init(const Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size}
{$ifdef DLL} stdcall; {$endif}
{$else}
procedure AES_XorBlock(var B1, B2: TAESBlock; var B3: TAESBlock);
{-xor two blocks, result in third}
function AES_Init(var Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size}
{$endif}
procedure AES_SetFastInit(value: boolean);
{-set FastInit variable}
{$ifdef DLL} stdcall; {$endif}
function AES_GetFastInit: boolean;
{-Returns FastInit variable}
{$ifdef DLL} stdcall; {$endif}
implementation
{$ifdef D4Plus}
var
{$else}
{$ifdef J_OPT} {$J+} {$endif}
const
{$endif}
FastInit : boolean = true; {Clear only necessary context data at init}
{IV and buf remain uninitialized}
const
RCon: array[0..9] of longint= ($01,$02,$04,$08,$10,$20,$40,$80,$1b,$36);
{$ifdef BASM16}
{---------------------------------------------------------------------------}
procedure AES_XorBlock({$ifdef CONST} const {$else} var {$endif} B1, B2: TAESBlock; var B3: TAESBlock);
{-xor two blocks, result in third}
begin
asm
mov di,ds
lds si,[B1]
db $66; mov ax,[si]
db $66; mov bx,[si+4]
db $66; mov cx,[si+8]
db $66; mov dx,[si+12]
lds si,[B2]
db $66; xor ax,[si]
db $66; xor bx,[si+4]
db $66; xor cx,[si+8]
db $66; xor dx,[si+12]
lds si,[B3]
db $66; mov [si],ax
db $66; mov [si+4],bx
db $66; mov [si+8],cx
db $66; mov [si+12],dx
mov ds,di
end;
end;
{$else}
{---------------------------------------------------------------------------}
procedure AES_XorBlock({$ifdef CONST} const {$else} var {$endif} B1, B2: TAESBlock; var B3: TAESBlock);
{-xor two blocks, result in third}
var
a1: TWA4 absolute B1;
a2: TWA4 absolute B2;
a3: TWA4 absolute B3;
begin
a3[0] := a1[0] xor a2[0];
a3[1] := a1[1] xor a2[1];
a3[2] := a1[2] xor a2[2];
a3[3] := a1[3] xor a2[3];
end;
{$endif BASM16}
{---------------------------------------------------------------------------}
function AES_Init({$ifdef CONST} const {$else} var {$endif} Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size}
var
pK: ^TAWK;
i : integer;
temp: longint;
{$ifdef BIT16}
s: TBA4;
t: TBA4 absolute temp;
{$endif}
Nk: word;
begin
AES_Init := 0;
if FastInit then with ctx do begin
{Clear only the necessary context data at init. IV and buf}
{remain uninitialized, other fields are initialized below.}
bLen :=0;
Flag :=0;
{$ifdef CONST}
IncProc := nil;
{$else}
{TP5-6 do not like IncProc := nil;}
fillchar(IncProc, sizeof(IncProc), 0);
{$endif}
end
else fillchar(ctx, sizeof(ctx), 0);
if (KeyBits<>128) and (KeyBits<>192) and (KeyBits<>256) then begin
AES_Init := AES_Err_Invalid_Key_Size;
exit;
end;
Nk := KeyBits div 32;
Move(Key, ctx.RK, 4*Nk);
ctx.KeyBits := KeyBits;
ctx.Rounds := 6 + Nk;
ctx.Decrypt := 0;
{Calculate encryption round keys, cf.[2]}
pK := addr(ctx.RK);
{$ifdef BIT16}
{16 bit: use byte arrays}
if keybits=128 then begin
for i:=0 to 9 do begin
temp := pK^[3];
{SubWord(RotWord(temp)) if "word" count mod 4 = 0}
s[0] := SBox[t[1]];
s[1] := SBox[t[2]];
s[2] := SBox[t[3]];
s[3] := SBox[t[0]];
pK^[4] := longint(s) xor pK^[0] xor RCon[i];
pK^[5] := pK^[1] xor pK^[4];
pK^[6] := pK^[2] xor pK^[5];
pK^[7] := pK^[3] xor pK^[6];
pK := addr(pK^[4]);
end;
end
else if keybits=192 then begin
for i:=0 to 7 do begin
temp := pK^[5];
{SubWord(RotWord(temp)) if "word" count mod 6 = 0}
s[0] := SBox[t[1]];
s[1] := SBox[t[2]];
s[2] := SBox[t[3]];
s[3] := SBox[t[0]];
pK^[ 6] := longint(s) xor pK^[0] xor RCon[i];
pK^[ 7] := pK^[1] xor pK^[6];
pK^[ 8] := pK^[2] xor pK^[7];
pK^[ 9] := pK^[3] xor pK^[8];
if i=7 then exit;
pK^[10] := pK^[4] xor pK^[ 9];
pK^[11] := pK^[5] xor pK^[10];
pK := addr(pK^[6]);
end;
end
else begin
for i:=0 to 6 do begin
temp := pK^[7];
{SubWord(RotWord(temp)) if "word" count mod 8 = 0}
s[0] := SBox[t[1]];
s[1] := SBox[t[2]];
s[2] := SBox[t[3]];
s[3] := SBox[t[0]];
pK^[ 8] := longint(s) xor pK^[0] xor RCon[i];
pK^[ 9] := pK^[1] xor pK^[ 8];
pK^[10] := pK^[2] xor pK^[ 9];
pK^[11] := pK^[3] xor pK^[10];
if i=6 then exit;
temp := pK^[11];
{SubWord(temp) if "word" count mod 8 = 4}
s[0] := SBox[t[0]];
s[1] := SBox[t[1]];
s[2] := SBox[t[2]];
s[3] := SBox[t[3]];
pK^[12] := longint(s) xor pK^[4];
pK^[13] := pK^[5] xor pK^[12];
pK^[14] := pK^[6] xor pK^[13];
pK^[15] := pK^[7] xor pK^[14];
pK := addr(pK^[8]);
end;
end;
{$else}
{32 bit use shift and mask}
if keybits=128 then begin
for i:=0 to 9 do begin
temp := pK^[3];
{SubWord(RotWord(temp)) if "word" count mod 4 = 0}
pK^[4] := (longint(SBox[(temp shr 8) and $ff]) ) xor
(longint(SBox[(temp shr 16) and $ff]) shl 8) xor
(longint(SBox[(temp shr 24) ]) shl 16) xor
(longint(SBox[(temp ) and $ff]) shl 24) xor
pK^[0] xor RCon[i];
pK^[5] := pK^[1] xor pK^[4];
pK^[6] := pK^[2] xor pK^[5];
pK^[7] := pK^[3] xor pK^[6];
pK := addr(pK^[4]);
end;
end
else if keybits=192 then begin
for i:=0 to 7 do begin
temp := pK^[5];
{SubWord(RotWord(temp)) if "word" count mod 6 = 0}
pK^[ 6] := (longint(SBox[(temp shr 8) and $ff]) ) xor
(longint(SBox[(temp shr 16) and $ff]) shl 8) xor
(longint(SBox[(temp shr 24) ]) shl 16) xor
(longint(SBox[(temp ) and $ff]) shl 24) xor
pK^[0] xor RCon[i];
pK^[ 7] := pK^[1] xor pK^[6];
pK^[ 8] := pK^[2] xor pK^[7];
pK^[ 9] := pK^[3] xor pK^[8];
if i=7 then exit;
pK^[10] := pK^[4] xor pK^[ 9];
pK^[11] := pK^[5] xor pK^[10];
pK := addr(pK^[6]);
end;
end
else begin
for i:=0 to 6 do begin
temp := pK^[7];
{SubWord(RotWord(temp)) if "word" count mod 8 = 0}
pK^[ 8] := (longint(SBox[(temp shr 8) and $ff]) ) xor
(longint(SBox[(temp shr 16) and $ff]) shl 8) xor
(longint(SBox[(temp shr 24) ]) shl 16) xor
(longint(SBox[(temp ) and $ff]) shl 24) xor
pK^[0] xor RCon[i];
pK^[ 9] := pK^[1] xor pK^[ 8];
pK^[10] := pK^[2] xor pK^[ 9];
pK^[11] := pK^[3] xor pK^[10];
if i=6 then exit;
temp := pK^[11];
{SubWord(temp) if "word" count mod 8 = 4}
pK^[12] := (longint(SBox[(temp ) and $ff]) ) xor
(longint(SBox[(temp shr 8) and $ff]) shl 8) xor
(longint(SBox[(temp shr 16) and $ff]) shl 16) xor
(longint(SBox[(temp shr 24) ]) shl 24) xor
pK^[4];
pK^[13] := pK^[5] xor pK^[12];
pK^[14] := pK^[6] xor pK^[13];
pK^[15] := pK^[7] xor pK^[14];
pK := addr(pK^[8]);
end;
end;
{$endif}
end;
{---------------------------------------------------------------------------}
procedure AES_SetFastInit(value: boolean);
{-set FastInit variable}
begin
FastInit := value;
end;
{---------------------------------------------------------------------------}
function AES_GetFastInit: boolean;
{-Returns FastInit variable}
begin
AES_GetFastInit := FastInit;
end;
end.

View File

@ -0,0 +1,280 @@
unit AES_CBC;
(*************************************************************************
DESCRIPTION : AES CBC functions
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12, FPC, VP
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
REFERENCES : [3] http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
[1] http://csrc.nist.gov/fips/fips-197.pdf
[4] Cipher text stealing: Schneier, Applied Cryptography 2.ed, ch.9.3
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 20.09.03 we initial version
0.20 21.09.03 we Cipher text stealing
0.21 21.09.03 we with Flag, functions, error codes
0.22 27.09.03 we FPC/go32v2
0.23 03.10.03 we 3-para encr/decr
0.24 03.10.03 we Fix overwrite source bug for decrypt
0.25 05.10.03 we STD.INC, TP5-6
0.26 12.06.04 we uses BLKSIZE constant
0.27 12.06.04 we check for nil pointers
0.28 02.07.04 we {$ifdef DLL} stdcall; {$endif}
0.29 30.11.04 we AES_XorBlock, AESBLKSIZE
0.30 01.12.04 we No more processing after short block
0.31 09.07.06 we Checked: D9-D10
0.34 16.11.08 we Use Ptr2Inc from BTypes
0.35 27.07.10 we Longint ILen in AES_CBC_En/Decrypt
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2002-2010 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{$i STD.INC}
interface
uses
BTypes, AES_Type, AES_Base, AES_Encr, AES_Decr;
{$ifdef CONST}
function AES_CBC_Init_Encr(const Key; KeyBits: word; const IV: TAESBlock; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size, encrypt IV}
{$ifdef DLL} stdcall; {$endif}
function AES_CBC_Init_Decr(const Key; KeyBits: word; const IV: TAESBlock; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size, encrypt IV}
{$ifdef DLL} stdcall; {$endif}
{$else}
function AES_CBC_Init_Encr(var Key; KeyBits: word; var IV: TAESBlock; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size, encrypt IV}
function AES_CBC_Init_Decr(var Key; KeyBits: word; var IV: TAESBlock; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size, encrypt IV}
{$endif}
function AES_CBC_Encrypt(ptp, ctp: Pointer; ILen: longint; var ctx: TAESContext): integer;
{-Encrypt ILen bytes from ptp^ to ctp^ in CBC mode}
{$ifdef DLL} stdcall; {$endif}
function AES_CBC_Decrypt(ctp, ptp: Pointer; ILen: longint; var ctx: TAESContext): integer;
{-Decrypt ILen bytes from ctp^ to ptp^ in CBC mode}
{$ifdef DLL} stdcall; {$endif}
implementation
{---------------------------------------------------------------------------}
{$ifdef CONST}
function AES_CBC_Init_Encr(const Key; KeyBits: word; const IV: TAESBlock; var ctx: TAESContext): integer;
{$else}
function AES_CBC_Init_Encr(var Key; KeyBits: word; var IV: TAESBlock; var ctx: TAESContext): integer;
{$endif}
{-AES key expansion, error if invalid key size, encrypt IV}
begin
{-AES key expansion, error if invalid key size}
AES_CBC_Init_Encr := AES_Init_Encr(Key, KeyBits, ctx);
ctx.IV := IV;
end;
{---------------------------------------------------------------------------}
{$ifdef CONST}
function AES_CBC_Init_Decr(const Key; KeyBits: word; const IV: TAESBlock; var ctx: TAESContext): integer;
{$else}
function AES_CBC_Init_Decr(var Key; KeyBits: word; var IV: TAESBlock; var ctx: TAESContext): integer;
{$endif}
{-AES key expansion, error if invalid key size, encrypt IV}
begin
{-AES key expansion, error if invalid key size}
AES_CBC_Init_Decr := AES_Init_Decr(Key, KeyBits, ctx);
ctx.IV := IV;
end;
{---------------------------------------------------------------------------}
function AES_CBC_Encrypt(ptp, ctp: Pointer; ILen: longint; var ctx: TAESContext): integer;
{-Encrypt ILen bytes from ptp^ to ctp^ in CBC mode}
var
i,n: longint;
m: word;
begin
AES_CBC_Encrypt := 0;
if ILen<0 then ILen := 0;
if ctx.Decrypt<>0 then begin
AES_CBC_Encrypt := AES_Err_Invalid_Mode;
exit;
end;
if (ptp=nil) or (ctp=nil) then begin
if ILen>0 then begin
AES_CBC_Encrypt := AES_Err_NIL_Pointer;
exit;
end;
end;
{$ifdef BIT16}
if (ofs(ptp^)+ILen>$FFFF) or (ofs(ctp^)+ILen>$FFFF) then begin
AES_CBC_Encrypt := AES_Err_Invalid_16Bit_Length;
exit;
end;
{$endif}
n := ILen div AESBLKSIZE; {Full blocks}
m := ILen mod AESBLKSIZE; {Remaining bytes in short block}
if m<>0 then begin
if n=0 then begin
AES_CBC_Encrypt := AES_Err_Invalid_Length;
exit;
end;
dec(n); {CTS: special treatment of last TWO blocks}
end;
{Short block must be last, no more processing allowed}
if ctx.Flag and 1 <> 0 then begin
AES_CBC_Encrypt := AES_Err_Data_After_Short_Block;
exit;
end;
with ctx do begin
for i:=1 to n do begin
{ct[i] = encr(ct[i-1] xor pt[i]), cf. [3] 6.2}
AES_XorBlock(PAESBlock(ptp)^, IV, IV);
AES_Encrypt(ctx, IV, IV);
PAESBlock(ctp)^ := IV;
inc(Ptr2Inc(ptp),AESBLKSIZE);
inc(Ptr2Inc(ctp),AESBLKSIZE);
end;
if m<>0 then begin
{Cipher text stealing}
AES_XorBlock(PAESBlock(ptp)^, IV, IV);
AES_Encrypt(ctx, IV, IV);
buf := IV;
inc(Ptr2Inc(ptp),AESBLKSIZE);
for i:=0 to m-1 do IV[i] := IV[i] xor PAESBlock(ptp)^[i];
AES_Encrypt(ctx, IV, PAESBlock(ctp)^);
inc(Ptr2Inc(ctp),AESBLKSIZE);
move(buf,PAESBlock(ctp)^,m);
{Set short block flag}
Flag := Flag or 1;
end;
end;
end;
{---------------------------------------------------------------------------}
function AES_CBC_Decrypt(ctp, ptp: Pointer; ILen: longint; var ctx: TAESContext): integer;
{-Decrypt ILen bytes from ctp^ to ptp^ in CBC mode}
var
i,n: longint;
m: word;
tmp: TAESBlock;
begin
AES_CBC_Decrypt := 0;
if ILen<0 then ILen := 0;
if ctx.Decrypt=0 then begin
AES_CBC_Decrypt := AES_Err_Invalid_Mode;
exit;
end;
if (ptp=nil) or (ctp=nil) then begin
if ILen>0 then begin
AES_CBC_Decrypt := AES_Err_NIL_Pointer;
exit;
end;
end;
{$ifdef BIT16}
if (ofs(ptp^)+ILen>$FFFF) or (ofs(ctp^)+ILen>$FFFF) then begin
AES_CBC_Decrypt := AES_Err_Invalid_16Bit_Length;
exit;
end;
{$endif}
n := ILen div AESBLKSIZE; {Full blocks}
m := ILen mod AESBLKSIZE; {Remaining bytes in short block}
if m<>0 then begin
if n=0 then begin
AES_CBC_Decrypt := AES_Err_Invalid_Length;
exit;
end;
dec(n); {CTS: special treatment of last TWO blocks}
end;
{Short block must be last, no more processing allowed}
if ctx.Flag and 1 <> 0 then begin
AES_CBC_Decrypt := AES_Err_Data_After_Short_Block;
exit;
end;
with ctx do begin
for i:=1 to n do begin
{pt[i] = decr(ct[i]) xor ct[i-1]), cf. [3] 6.2}
buf := IV;
IV := PAESBlock(ctp)^;
AES_Decrypt(ctx, IV, PAESBlock(ptp)^);
AES_XorBlock(PAESBlock(ptp)^, buf, PAESBlock(ptp)^);
inc(Ptr2Inc(ptp),AESBLKSIZE);
inc(Ptr2Inc(ctp),AESBLKSIZE);
end;
if m<>0 then begin
{Cipher text stealing, L=ILen (Schneier's n)}
buf := IV; {C(L-2)}
AES_Decrypt(ctx, PAESBlock(ctp)^, IV);
inc(Ptr2Inc(ctp),AESBLKSIZE);
fillchar(tmp,sizeof(tmp),0);
move(PAESBlock(ctp)^,tmp,m); {c[L]|0}
AES_XorBlock(tmp,IV,IV);
tmp := IV;
move(PAESBlock(ctp)^,tmp,m); {c[L]| C'}
AES_Decrypt(ctx,tmp,tmp);
AES_XorBlock(tmp, buf, PAESBlock(ptp)^);
inc(Ptr2Inc(ptp),AESBLKSIZE);
move(IV,PAESBlock(ptp)^,m);
{Set short block flag}
Flag := Flag or 1;
end;
end;
end;
end.

View File

@ -0,0 +1,69 @@
(*************************************************************************
DESCRIPTION : AES configuration include file
REQUIREMENTS : TP5-7, D1-D7/D9-D10, FPC, VP (Undef BASM16 for 286)
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version
0.11 09.07.06 we Common defines for encrypt/decryt tables
0.12 19.07.06 we Cond. defines AES_Diag, AES_Encr/Decr_DummyAlign
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2006 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{Use additional 1K expanded able Te4/Td4 for last encryption round}
{.$define AES_LONGBOX}
{Use 2K tables TCe/TCd instead of four 1K tables Te0 .. Te3, Td0 .. Td3}
{.$define AES_ComprTab}
{Interface some diagnostic data, eg Enc/Dec table offsets mod 16}
{$define AES_Diag}
{Use to align TCe to 8 byte boundary, inserts dummy longint}
{Inspect the map file and/or use AES_Diag/TCe_Diag}
{.$define AES_Encr_DummyAlign}
{Use to align TCd to 8 byte boundary, inserts dummy longint}
{Inspect the map file and/or use AES_Diag/TCd_Diag}
{.$define AES_Decr_DummyAlign}
{---------------------------------------------------------------------------}
{Consistency check - do not change!}
{$ifdef AES_ComprTab}
{$ifdef AES_LONGBOX}
{$undef AES_LONGBOX}
{$endif}
{$endif}

View File

@ -0,0 +1,191 @@
unit AES_Decr;
(*************************************************************************
DESCRIPTION : AES decrypt functions
(not needed for CFB/CTR/OFB mode)
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12/D17, FPC, VP
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
REFERENCES : [1] http://csrc.nist.gov/fips/fips-197.pdf
[2] rijndael-alg-fst.c V2.0/3.0: Rijmen et al Aug1999/Dec2000
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.22 16.08.03 we longint statt word32
0.23 16.08.03 we separate aes_decr
0.24 16.08.03 we new xor_block
0.25 18.09.03 we Static tables, GF routines from aes_base, D4+
0.26 20.09.03 we optimized round code, no more move/if
0.27 21.09.03 we with Flag, functions, error codes
0.28 27.09.03 we without GFMul and -tables
0.29 27.09.03 we FPC/go32v2
0.30 28.09.03 we reorder round loop: gain 1 transformation t->block
0.31 28.09.03 we merge last xorblock
0.32 28.09.03 we two rounds in each loop
0.33 03.10.03 we 3-para encr/decr
0.34 03.10.03 we two local blocks if partial unroll
0.35 03.10.03 we BASM for BP7
0.36 04.10.03 we remove add di,4
0.37 05.10.03 we STD.INC, TP6
0.38 05.10.03 we TP5,TP5.5
0.39 28.12.03 we DPerm removed
0.40 29.12.03 we BASM16: Bugfix if seg(BO)<>ds, xorblock in asm
0.41 29.12.03 we Delphi/VP: Pointer version
0.42 29.12.03 we InvMixColumn with SBox,T5..T8, Bugfix
0.43 29.12.03 we InvMixColumn with TBA4 if not BIT32
0.44 15.01.04 we InvMixColumn inline
0.45 16.01.04 we MakeDecrKey as BIT32, BASM16, BIT16
0.46 14.08.04 we UseLongBox/Td4
0.47 30.11.04 we AES_XorBlock, AESBLKSIZE
0.48 24.12.04 we STD code and Td0..Td3 like [2], AES_DECR ifdefs
0.49 24.12.04 we New ifdef logic, move replacement code to inc
0.50 24.12.04 we TP5/5.5 with round key pointer
0.51 24.12.04 we Fully unrolled 32 bit in dec_full.inc
0.52 24.12.04 we BASM16: lea trick for 4*bx
0.53 25.12.04 we BIT32: rearrange loop for descending key access
0.54 27.12.04 we All: rearrange loop for descending key access
0.55 04.03.05 we FPC 1.9.8, STD.INC V1.10, StrictLong
0.56 05.05.05 we $R- for StrictLong, D9: errors if $R+ even if warnings off
0.57 09.07.06 we Compressed tables, code in INC files
0.58 19.07.06 we TCd_Diag
0.59 21.11.08 we Use __P2I from BTypes
0.60 01.12.12 we separate BIT64 include statements
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2002-2012 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{$i STD.INC}
interface
uses AES_Type, AES_Base;
{$i aes_conf.inc}
{$ifdef AES_Diag}
{$ifdef AES_ComprTab}
var
TCd_Diag: integer; {offset of TCd table mod 15}
{should be 0 or 8 for optimal alignment}
{$endif}
{$endif}
{$ifdef CONST}
function AES_Init_Decr(const Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, InvMixColumn(Key) for Decypt, error if invalid key size}
{$ifdef DLL} stdcall; {$endif}
procedure AES_Decrypt(var ctx: TAESContext; const BI: TAESBlock; var BO: TAESBlock);
{-decrypt one block (in ECB mode)}
{$ifdef DLL} stdcall; {$endif}
{$else}
function AES_Init_Decr(var Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, InvMixColumn(Key) for Decypt, error if invalid key size}
procedure AES_Decrypt(var ctx: TAESContext; var BI: TAESBlock; var BO: TAESBlock);
{-decrypt one block (in ECB mode)}
{$endif}
implementation
uses BTypes;
type
PLong = ^longint;
{$ifdef AES_ComprTab}
{$i dec_cdat.inc}
{$ifndef BIT16}
{$ifdef BIT64}
{$i dec_cp16.inc} {This version is faster for FPC260/Win7-64!!!}
{$else}
{$i dec_cp32.inc}
{$endif}
{$else}
{$ifdef BASM16}
{$i dec_ca16.inc}
{$else}
{$i dec_cp16.inc}
{$endif}
{$endif}
{$else}
{$i dec_fdat.inc}
{$ifndef BIT16}
{$ifdef BIT64}
{$i dec_fp16.inc} {This version is faster for FPC260/Win7-64!!!}
{$else}
{$i dec_fp32.inc}
{$endif}
{$else}
{$ifdef BASM16}
{$i dec_fa16.inc}
{$else}
{$i dec_fp16.inc}
{$endif}
{$endif}
{$endif}
{---------------------------------------------------------------------------}
function AES_Init_Decr({$ifdef CONST} const {$else} var {$endif} Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, InvMixColumn(Key) for decrypt, error if invalid key size}
begin
AES_Init_Decr := AES_Init(Key, KeyBits, ctx);
MakeDecrKey(ctx);
ctx.Decrypt := 1;
end;
{$ifdef AES_ComprTab}
begin
{$ifdef AES_Diag}
TCd_Diag := __P2I(@TCd) and 15;
{$endif}
{$ifdef AES_Decr_DummyAlign}
if TCdDummy<>0 then ;
{$endif}
{$endif}
end.

View File

@ -0,0 +1,250 @@
unit AES_ECB;
(*************************************************************************
DESCRIPTION : AES ECB functions
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12, FPC, VP
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
REFERENCES : [3] http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
[1] http://csrc.nist.gov/fips/fips-197.pdf
[4] Cipher text stealing: Schneier, Applied Cryptography 2.ed, ch.9.1
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 21.09.03 we initial version a la CBC
0.11 27.09.03 we FPC/go32v2
0.12 03.10.03 we 3-para encr/decr
0.13 05.10.03 we STD.INC, TP5-6
0.14 12.06.04 we uses BLKSIZE constant
0.15 12.06.04 we check for nil pointers
0.16 02.07.04 we {$ifdef DLL} stdcall; {$endif}
0.17 30.11.04 we AES_XorBlock, AESBLKSIZE
0.18 01.12.04 we No more processing after short block
0.19 09.07.06 we Checked: D9-D10
0.20 15.11.08 we Use Ptr2Inc from BTypes
0.21 27.07.10 we Longint ILen in AES_ECB_En/Decrypt
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2002-2010 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{$i STD.INC}
interface
uses
BTypes, AES_Type, AES_Base, AES_Encr, AES_Decr;
{$ifdef CONST}
function AES_ECB_Init_Encr(const Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size, encrypt IV}
{$ifdef DLL} stdcall; {$endif}
function AES_ECB_Init_Decr(const Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size, encrypt IV}
{$ifdef DLL} stdcall; {$endif}
{$else}
function AES_ECB_Init_Encr(var Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size, encrypt IV}
function AES_ECB_Init_Decr(var Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size, encrypt IV}
{$endif}
function AES_ECB_Encrypt(ptp, ctp: Pointer; ILen: longint; var ctx: TAESContext): integer;
{-Encrypt ILen bytes from ptp^ to ctp^ in ECB mode}
{$ifdef DLL} stdcall; {$endif}
function AES_ECB_Decrypt(ctp, ptp: Pointer; ILen: longint; var ctx: TAESContext): integer;
{-Decrypt ILen bytes from ctp^ to ptp^ in ECB mode}
{$ifdef DLL} stdcall; {$endif}
implementation
{---------------------------------------------------------------------------}
function AES_ECB_Init_Encr({$ifdef CONST} const {$else} var {$endif} Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size}
begin
{-AES key expansion, error if invalid key size}
AES_ECB_Init_Encr := AES_Init_Encr(Key, KeyBits, ctx);
end;
{---------------------------------------------------------------------------}
function AES_ECB_Init_Decr({$ifdef CONST} const {$else} var {$endif} Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size}
begin
{-AES key expansion, error if invalid key size}
AES_ECB_Init_Decr := AES_Init_Decr(Key, KeyBits, ctx);
end;
{---------------------------------------------------------------------------}
function AES_ECB_Encrypt(ptp, ctp: Pointer; ILen: longint; var ctx: TAESContext): integer;
{-Encrypt ILen bytes from ptp^ to ctp^ in ECB mode}
var
i,n: longint;
m: word;
tmp: TAESBlock;
begin
AES_ECB_Encrypt := 0;
if ILen<0 then ILen := 0;
if ctx.Decrypt<>0 then begin
AES_ECB_Encrypt := AES_Err_Invalid_Mode;
exit;
end;
if (ptp=nil) or (ctp=nil) then begin
if ILen>0 then begin
AES_ECB_Encrypt := AES_Err_NIL_Pointer;
exit;
end;
end;
{$ifdef BIT16}
if (ofs(ptp^)+ILen>$FFFF) or (ofs(ctp^)+ILen>$FFFF) then begin
AES_ECB_Encrypt := AES_Err_Invalid_16Bit_Length;
exit;
end;
{$endif}
n := ILen div AESBLKSIZE; {Full blocks}
m := ILen mod AESBLKSIZE; {Remaining bytes in short block}
if m<>0 then begin
if n=0 then begin
AES_ECB_Encrypt := AES_Err_Invalid_Length;
exit;
end;
dec(n); {CTS: special treatment of last TWO blocks}
end;
{Short block must be last, no more processing allowed}
if ctx.Flag and 1 <> 0 then begin
AES_ECB_Encrypt := AES_Err_Data_After_Short_Block;
exit;
end;
with ctx do begin
for i:=1 to n do begin
AES_Encrypt(ctx, PAESBlock(ptp)^, PAESBlock(ctp)^);
inc(Ptr2Inc(ptp),AESBLKSIZE);
inc(Ptr2Inc(ctp),AESBLKSIZE);
end;
if m<>0 then begin
{Cipher text stealing}
AES_Encrypt(ctx, PAESBlock(ptp)^, buf);
inc(Ptr2Inc(ptp),AESBLKSIZE);
tmp := buf;
move(PAESBlock(ptp)^, tmp, m);
AES_Encrypt(ctx, tmp, PAESBlock(ctp)^);
inc(Ptr2Inc(ctp),AESBLKSIZE);
move(buf,PAESBlock(ctp)^,m);
{Set short block flag}
Flag := Flag or 1;
end;
end;
end;
{---------------------------------------------------------------------------}
function AES_ECB_Decrypt(ctp, ptp: Pointer; ILen: longint; var ctx: TAESContext): integer;
{-Decrypt ILen bytes from ctp^ to ptp^ in ECB mode}
var
i,n: longint;
m: word;
tmp: TAESBlock;
begin
AES_ECB_Decrypt := 0;
if ILen<0 then ILen := 0;
if ctx.Decrypt=0 then begin
AES_ECB_Decrypt := AES_Err_Invalid_Mode;
exit;
end;
if (ptp=nil) or (ctp=nil) then begin
if ILen>0 then begin
AES_ECB_Decrypt := AES_Err_NIL_Pointer;
exit;
end;
end;
{$ifdef BIT16}
if (ofs(ptp^)+ILen>$FFFF) or (ofs(ctp^)+ILen>$FFFF) then begin
AES_ECB_Decrypt := AES_Err_Invalid_16Bit_Length;
exit;
end;
{$endif}
n := ILen div AESBLKSIZE; {Full blocks}
m := ILen mod AESBLKSIZE; {Remaining bytes in short block}
if m<>0 then begin
if n=0 then begin
AES_ECB_Decrypt := AES_Err_Invalid_Length;
exit;
end;
dec(n); {CTS: special treatment of last TWO blocks}
end;
{Short block must be last, no more processing allowed}
if ctx.Flag and 1 <> 0 then begin
AES_ECB_Decrypt := AES_Err_Data_After_Short_Block;
exit;
end;
with ctx do begin
for i:=1 to n do begin
AES_Decrypt(ctx, PAESBlock(ctp)^, PAESBlock(ptp)^);
inc(Ptr2Inc(ptp),AESBLKSIZE);
inc(Ptr2Inc(ctp),AESBLKSIZE);
end;
if m<>0 then begin
{Cipher text stealing}
AES_Decrypt(ctx, PAESBlock(ctp)^, buf);
inc(Ptr2Inc(ctp),AESBLKSIZE);
tmp := buf;
move(PAESBlock(ctp)^, tmp, m);
AES_Decrypt(ctx, tmp, PAESBlock(ptp)^);
inc(Ptr2Inc(ptp),AESBLKSIZE);
move(buf,PAESBlock(ptp)^,m);
{Set short block flag}
Flag := Flag or 1;
end;
end;
end;
end.

View File

@ -0,0 +1,180 @@
unit AES_Encr;
(*************************************************************************
DESCRIPTION : AES encrypt functions
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12/D17, FPC, VP
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
REFERENCES : [1] http://csrc.nist.gov/fips/fips-197.pdf
[2] rijndael-alg-fst.c V2.0/3.0: Rijmen et al Aug1999/Dec2000
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.22 16.08.03 we longint statt word32
0.23 16.08.03 we separate aes_encr
0.24 16.08.03 we new xor_block
0.25 18.09.03 we Static tables, D4+
0.26 20.09.03 we optimized round code, no more move/if
0.27 21.09.03 we functions, error codes
0.28 27.09.03 we FPC/go32v2
0.29 28.09.03 we removed temporary s-Block
0.30 28.09.03 we two rounds in each loop, merge last xorblock
0.31 03.10.03 we 3-para encr/decr
0.32 03.10.03 we two local blocks if partial unroll
0.33 03.10.03 we BASM for BP7
0.34 04.10.03 we remove add di,4
0.35 05.10.03 we STD.INC, TP6
0.36 05.10.03 we TP5,TP5.5
0.37 27.12.03 we EPerm removed
0.38 28.12.03 we Delphi/VP: Pointer version
BASM16: changed variable order
0.39 28.12.03 we BASM16: SBox code in asm,
PTR: merge SBox code with XOR RK
0.40 29.12.03 we BASM16: xorblock in asm, PTR: reorder
0.41 02.07.04 we {$ifdef DLL} stdcall; {$endif}
0.42 14.08.04 we UseLongBox/Te4
0.43 30.11.04 we AES_XorBlock, AESBLKSIZE
0.44 24.12.04 we STD code and Te0..Te3 like [2], AES_ENCR ifdefs
0.45 24.12.04 we New ifdef logic, move replacement code to inc
0.46 24.12.04 we TP5/5.5 with round key pointer
0.47 24.12.04 we BASM16: lea trick for 4*bx
0.48 04.03.05 we FPC 1.9.8, STD.INC V1.10, StrictLong
0.49 05.05.05 we $R- for StrictLong, D9: errors if $R+ even if warnings off
0.50 09.07.06 we Compressed tables, code in INC files
0.51 19.07.06 we TCe_Diag
0.52 21.11.08 we Use __P2I from BTypes
0.53 01.12.12 we separate BIT64 include statements
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2002-2012 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{$i STD.INC}
interface
uses
AES_Type, AES_Base;
{$i aes_conf.inc}
{$ifdef AES_Diag}
{$ifdef AES_ComprTab}
var
TCe_Diag: integer; {offset of TCe table mod 15}
{should be 0 or 8 for optimal alignment}
{$endif}
{$endif}
{$ifdef CONST}
function AES_Init_Encr(const Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size}
{$ifdef DLL} stdcall; {$endif}
procedure AES_Encrypt(var ctx: TAESContext; const BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
{$ifdef DLL} stdcall; {$endif}
{$else}
function AES_Init_Encr(var Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size}
procedure AES_Encrypt(var ctx: TAESContext; var BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
{$endif}
implementation
uses BTypes;
{$ifdef AES_ComprTab}
{$i enc_cdat.inc}
{$ifndef BIT16}
{$ifdef BIT64}
{$i enc_cp16.inc} {This version is faster for FPC260/Win7-64!!!}
{$else}
{$i enc_cp32.inc}
{$endif}
{$else}
{$ifdef BASM16}
{$i enc_ca16.inc}
{$else}
{$i enc_cp16.inc}
{$endif}
{$endif}
{$else}
{$i enc_fdat.inc}
{$ifndef BIT16}
{$ifdef BIT64}
{$i enc_fp16.inc} {This version is faster for FPC260/Win7-64!!!}
{$else}
{$i enc_fp32.inc}
{$endif}
{$else}
{$ifdef BASM16}
{$i enc_fa16.inc}
{$else}
{$i enc_fp16.inc}
{$endif}
{$endif}
{$endif}
{---------------------------------------------------------------------------}
function AES_Init_Encr({$ifdef CONST} const {$else} var {$endif} Key; KeyBits: word; var ctx: TAESContext): integer;
{-AES key expansion, error if invalid key size}
begin
AES_Init_Encr := AES_Init(Key, KeyBits, ctx);
end;
{$ifdef AES_ComprTab}
begin
{$ifdef AES_Diag}
TCe_Diag := __P2I(@TCe) and 15;
{$endif}
{$ifdef AES_Encr_DummyAlign}
if TCeDummy<>0 then ;
{$endif}
{$endif}
end.

View File

@ -0,0 +1,150 @@
(*************************************************************************
Include file for AES CTR Seek routines. These routines are in a separate inc
file because they are included in aes_ctr.pas for non-dlls AND in the dll
interface units AES_Intv/AES_Intf. This is done in order to make tests like
@IncProc=@AES_IncMSBPart work without additional overhead in programs using
the dll.
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 31.07.10 W.Ehrhardt Initial version
**************************************************************************)
(****** (C) Copyright 2010 Wolfgang Ehrhardt -- see copying_we.txt ******)
{---------------------------------------------------------------------------}
function AES_CTR_Seek({$ifdef CONST}const{$else}var{$endif} iCTR: TAESBlock;
SOL, SOH: longint; var ctx: TAESContext): integer;
{-Setup ctx for random access crypto stream starting at 64 bit offset SOH*2^32+SOL,}
{ SOH >= 0. iCTR is the initial CTR for offset 0, i.e. the same as in AES_CTR_Init.}
var
i,pt: integer;
carry: word;
TC: TAESBlock;
type
TWA4 = packed array[0..3] of longint; {AES block as array of longint}
TBA4 = packed array[0..3] of byte; {AES "word" as array of byte }
begin
{WARNING: CTR mode demands that the same key / iCTR pair is never reused }
{for encryption. This requirement is especially important for the CTR_Seek}
{function. If different data is written to the same position there will be}
{leakage of information about the plaintexts. Therefore CTR_Seek should }
{normally be used for random reads only.}
if SOH < 0 then begin
AES_CTR_Seek := AES_Err_CTR_SeekOffset;
exit;
end
else with ctx do begin
blen := word(SOL) and $0F;
{64 bit shift right (SOH, SOL) 4 bits}
SOL := (SOL shr 4) or ((SOH and $0F) shl 28);
SOH := (SOH shr 4);
{Check if known IncProc}
{$ifdef FPC_ProcVar}
if (IncProc=nil) or (IncProc=@AES_IncMSBFull) then pt := 1
else if IncProc=@AES_IncMSBPart then pt := 2
else if IncProc=@AES_IncLSBFull then pt := 3
else if IncProc=@AES_IncLSBPart then pt := 4
else pt := 0;
{$else}
if (@IncProc=nil) or (@IncProc=@AES_IncMSBFull) then pt := 1
else if @IncProc=@AES_IncMSBPart then pt := 2
else if @IncProc=@AES_IncLSBFull then pt := 3
else if @IncProc=@AES_IncLSBPart then pt := 4
else pt := 0;
{$endif}
IV := iCTR;
if (SOL or SOH) <> 0 then begin
if pt=0 then begin
{No shortcut calculation for user-defined IncProcs. Note: SOH is }
{positive here even if the sign bit of the original SOH was set. }
{The execution of this loop may be very time-consuming because the }
{IncProc is called many times. If the user is able to calculate the}
{value IVo of the iCTR after calling IncProc (offset div 16) times,}
{invoking the function with AES_CTR_Seek(IVo, SOL and 15, 0, ctx) }
{will completely skip the IncProc calculation, but set the correct }
{values for ctx.IV, ctx.buf, and ctx.blen.}
if SOL=0 then dec(SOH);
repeat
repeat
IncProc(IV);
dec(SOL);
until SOL=0;
dec(SOH);
until SOH<=0;
end
else begin
fillchar(TC, sizeof(TC), 0);
carry := 0;
if (pt=1) or (pt=2) then begin
{MSB functions, first fill 128 bit offset vector}
for i:=0 to 3 do begin
TC[15-i] := TBA4(SOL)[i];
TC[11-i] := TBA4(SOH)[i];
end;
{64 bit addition}
for i:=15 downto 8 do begin
carry := carry + TC[i] + IV[i];
IV[i] := carry and $FF;
carry := carry shr 8;
end;
if (pt=1) and (carry<>0) then begin
{"Full" function: propagate carry through remaining bytes}
for i:=7 downto 0 do begin
carry := carry + IV[i];
IV[i] := carry and $FF;
carry := carry shr 8;
{$ifdef CONST}
if carry=0 then break;
{$endif}
end;
end;
end
else begin
{LSB functions, first fill 128 bit offset vector}
TWA4(TC)[0] := SOL;
TWA4(TC)[1] := SOH;
{64 bit addition}
for i:=0 to 7 do begin
carry := carry + TC[i] + IV[i];
IV[i] := carry and $FF;
carry := carry shr 8;
end;
if (pt=3) and (carry<>0) then begin
{"Full" function: propagate carry through remaining bytes}
for i:=8 to 15 do begin
carry := carry + IV[i];
IV[i] := carry and $FF;
carry := carry shr 8;
{$ifdef CONST}
if carry=0 then break;
{$endif}
end;
end;
end;
end;
end;
AES_Encrypt(ctx, IV, buf);
AES_CTR_Seek := 0;
end;
end;
{$ifdef HAS_INT64}
{$ifndef DLL}
{-----------------------------------------------------------------------------}
function AES_CTR_Seek64(const iCTR: TAESBlock; SO: int64; var ctx: TAESContext): integer;
{-Setup ctx for random access crypto stream starting at 64 bit offset SO >= 0;}
{ iCTR is the initial CTR value for offset 0, i.e. the same as in AES_CTR_Init.}
type
LH = packed record L,H: longint; end;
begin
AES_CTR_Seek64 := AES_CTR_Seek(iCTR, LH(SO).L, LH(SO).H, ctx);
end;
{$endif}
{$endif}

View File

@ -0,0 +1,119 @@
unit AES_Type;
(*************************************************************************
DESCRIPTION : AES type definitions
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12, FPC, VP
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
REFERENCES : ---
Version Date Author Modification
------- -------- ------- ------------------------------------------
1.00 16.08.03 we Sepatate unit from AESCrypt
1.10 15.09.03 we with IncProc
1.20 21.09.03 we with Flag, error codes
1.21 05.10.03 we with STD.INC
1.23 05.10.03 we with AES_Err_MultipleIncProcs
1.24 12.06.04 we with AES_Err_NIL_Pointer, const BLKSIZE
1.25 02.07.04 we {$ifdef DLL} stdcall; {$endif}
1.26 29.11.04 we FastInit
1.27 30.11.04 we AES_XorBlock, AESBLKSIZE
1.28 01.12.04 we AES_Err_Data_After_Short_Block
1.29 09.07.06 we Checked: D9-D10
1.30 20.07.08 we Error codes for EAX all-in-one function results
1.31 21.05.09 we CCM error codes
1.32 20.06.10 we CTR_Seek error code
1.33 27.07.10 we AES_Err_Invalid_16Bit_Length
1.34 27.09.10 we GCM error codes
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2002-2010 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
interface
const
AESMaxRounds = 14;
const
AES_Err_Invalid_Key_Size = -1; {Key size <> 128, 192, or 256 Bits}
AES_Err_Invalid_Mode = -2; {Encr/Decr with Init for Decr/Encr}
AES_Err_Invalid_Length = -3; {No full block for cipher stealing}
AES_Err_Data_After_Short_Block = -4; {Short block must be last }
AES_Err_MultipleIncProcs = -5; {More than one IncProc Setting }
AES_Err_NIL_Pointer = -6; {nil pointer to block with nonzero length}
AES_Err_EAX_Inv_Text_Length = -7; {More than 64K text length in EAX all-in-one for 16 Bit}
AES_Err_EAX_Inv_TAG_Length = -8; {EAX all-in-one tag length not 0..16}
AES_Err_EAX_Verify_Tag = -9; {EAX all-in-one tag does not compare}
AES_Err_CCM_Hdr_length = -10; {CCM header length >= $FF00}
AES_Err_CCM_Nonce_length = -11; {CCM nonce length < 7 or > 13}
AES_Err_CCM_Tag_length = -12; {CCM tag length not in [4,6,8,19,12,14,16]}
AES_Err_CCM_Verify_Tag = -13; {Computed CCM tag does not compare}
AES_Err_CCM_Text_length = -14; {16 bit plain/cipher text length to large}
AES_Err_CTR_SeekOffset = -15; {Negative offset in AES_CTR_Seek}
AES_Err_GCM_Verify_Tag = -17; {GCM all-in-one tag does not compare}
AES_Err_GCM_Auth_After_Final = -18; {Auth after final or multiple finals}
AES_Err_Invalid_16Bit_Length = -20; {BaseAddr + length > $FFFF for 16 bit code}
type
TAESBlock = packed array[0..15] of byte;
PAESBlock = ^TAESBlock;
TKeyArray = packed array[0..AESMaxRounds] of TAESBlock;
TIncProc = procedure(var CTR: TAESBlock); {user supplied IncCTR proc}
{$ifdef DLL} stdcall; {$endif}
TAESContext = packed record
RK : TKeyArray; {Key (encr. or decr.) }
IV : TAESBlock; {IV or CTR }
buf : TAESBlock; {Work buffer }
bLen : word; {Bytes used in buf }
Rounds : word; {Number of rounds }
KeyBits : word; {Number of bits in key }
Decrypt : byte; {<>0 if decrypting key }
Flag : byte; {Bit 1: Short block }
IncProc : TIncProc; {Increment proc CTR-Mode}
end;
const
AESBLKSIZE = sizeof(TAESBlock);
implementation
end.

View File

@ -0,0 +1,199 @@
unit BTypes;
{Common basic type definitions}
interface
{$i STD.INC}
(*************************************************************************
DESCRIPTION : Common basic type definitions
REQUIREMENTS : TP5-7, D1-D7/D9-D12/D17-D22, FPC, VP, WDOSX
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
REFERENCES : ---
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 15.04.06 W.Ehrhardt Initial version
0.11 15.04.06 we With $ifdef HAS_XTYPES
0.12 15.04.06 we FPC1_0 and pShortInt
0.13 09.09.08 we UInt32 = cardinal $ifdef HAS_CARD32
0.14 12.11.08 we Str127, Ptr2Inc
0.15 14.11.08 we BString, char8
0.16 21.11.08 we __P2I: type cast pointer to integer for masking etc
0.17 02.12.08 we Use pchar and pAnsiChar for pchar8 if possible
0.18 27.02.09 we pBoolean
0.19 14.02.12 we extended = double $ifdef SIMULATE_EXT64
0.20 06.05.14 we extended = double $ifdef SIMULATE_EXT64 OR EXT64
0.21 25.04.15 we With $ifdef HAS_INTXX, HAS_PINTXX
*************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2006-2015 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{$ifdef BIT16}
type
Int8 = ShortInt; { 8 bit signed integer}
Int16 = Integer; {16 bit signed integer}
Int32 = Longint; {32 bit signed integer}
UInt8 = Byte; { 8 bit unsigned integer}
UInt16 = Word; {16 bit unsigned integer}
UInt32 = Longint; {32 bit unsigned integer}
Smallint = Integer;
Shortstring = string;
pByte = ^Byte;
pBoolean = ^Boolean;
pShortInt = ^ShortInt;
pWord = ^Word;
pSmallInt = ^SmallInt;
pLongint = ^Longint;
{$else}
{$ifndef HAS_INTXX}
type
Int8 = ShortInt; { 8 bit signed integer}
Int16 = SmallInt; {16 bit signed integer}
Int32 = Longint; {32 bit signed integer}
UInt8 = Byte; { 8 bit unsigned integer}
UInt16 = Word; {16 bit unsigned integer}
{$ifdef HAS_CARD32}
UInt32 = Cardinal; {32 bit unsigned integer}
{$else}
UInt32 = Longint; {32 bit unsigned integer}
{$endif}
{$endif}
{$ifndef HAS_XTYPES}
type
pByte = ^Byte;
pBoolean = ^Boolean;
pShortInt = ^ShortInt;
pWord = ^Word;
pSmallInt = ^SmallInt;
pLongint = ^Longint;
{$endif}
{$ifdef FPC} {$ifdef VER1_0}
type
pBoolean = ^Boolean;
pShortInt = ^ShortInt;
{$endif} {$endif}
{$endif} {BIT16}
type
Str255 = string[255]; {Handy type to avoid problems with 32 bit and/or unicode}
Str127 = string[127];
type
{$ifndef HAS_PINTXX}
pInt8 = ^Int8;
pInt16 = ^Int16;
pInt32 = ^Int32;
pUInt8 = ^UInt8;
pUInt16 = ^UInt16;
pUInt32 = ^UInt32;
{$endif}
pStr255 = ^Str255;
pStr127 = ^Str127;
{$ifdef BIT16}
{$ifdef V7Plus}
type
BString = string[255]; {String of 8 bit characters}
pBString = ^BString;
char8 = char; {8 bit characters}
pchar8 = pchar;
{$else}
type
BString = string[255]; {String of 8 bit characters}
pBString = ^BString;
char8 = char; {8 bit characters}
pchar8 = ^char;
{$endif}
{$else}
{$ifdef UNICODE}
type
BString = AnsiString; {String of 8 bit characters}
pBString = pAnsiString;
char8 = AnsiChar; {8 bit characters}
pchar8 = pAnsiChar;
{$else}
type
BString = AnsiString; {String of 8 bit characters}
pBString = pAnsiString;
char8 = AnsiChar; {8 bit characters}
pchar8 = pAnsiChar;
{$endif}
{$endif}
{$ifdef V7Plus}
type
Ptr2Inc = pByte; {Type cast to increment untyped pointer}
{$else}
type
Ptr2Inc = Longint; {Type cast to increment untyped pointer}
{$endif}
{$ifdef FPC}
{$ifdef VER1}
type __P2I = longint; {Type cast pointer to integer for masking etc}
{$else}
type __P2I = PtrUInt; {Type cast pointer to integer for masking etc}
{$endif}
{$else}
{$ifdef BIT64}
type __P2I = NativeInt; {Type cast pointer to integer for masking etc}
{$else}
type __P2I = longint; {Type cast pointer to integer for masking etc}
{$endif}
{$endif}
{$ifdef EXT64}
type extended = double; {Force 64-bit 'extended'}
{$else}
{$ifdef SIMULATE_EXT64}
type extended = double; {Debug simulation EXT64}
{$endif}
{$endif}
implementation
end.

View File

@ -0,0 +1,50 @@
(C) Copyright 2002-2017 Wolfgang Ehrhardt
Based on "The zlib/libpng License":
http://www.opensource.org/licenses/zlib-license.php
__________________
COPYING CONDITIONS
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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
_______________________________________
Bedingungen fuer Nutzung und Weitergabe
Die Software (Quellcodes und Binaerdateien) wird ohne jegliche Zusagen
oder Garantien bezueglich Funktionalitaet oder Funktionsfaehigkeit
abgegeben. Die Autoren uebernehmen keine Verantwortung fuer Schaeden, die
durch die Benutzung der Software verursacht werden.
Die Software darf frei verwendet und weitergegeben werden (kommerzielle
Nutzung/Weitergabe ist erlaubt), vorausgesetzt die folgenden Bedingungen
werden eingehalten:
1. Die Herkunft der Software darf nicht falsch angegeben werden; es ist
nicht erlaubt, die Software als Werk eines anderen auszugeben. Wird die
Software in Teilen oder als Ganzes in einem Produkt benutzt, so ist
Hinweis auf die Herkunft in der Dokumentation erwuenscht, aber nicht
notwendig.
2. Geaenderte Quellcodes muessen deutlich als solche gekennzeichnet werden
und duerfen nicht als die Originalsoftware ausgegeben werden.
3. Die Bedingungen ueber die Nutzung/Weitergabe duerfen nicht entfernt oder
geaendert werden.

View File

@ -0,0 +1,397 @@
(*************************************************************************
Include file for AES_DECR.PAS - AES_Decrypt for BASM16/Compressed table
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version for compressed table
0.11 10.07.06 we Removed bx in TCd[bx+si+?]
0.12 13.07.06 we Uses TCd box byte instead of InvSBox
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{16 bit BASM used for TP6, BP7, Delphi1}
{---------------------------------------------------------------------------}
procedure AES_Decrypt(var ctx: TAESContext; {$ifdef CONST} const {$else} var {$endif} BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
var
s,t: TAESBlock;
r: integer;
pK: pointer;
begin
r := ctx.Rounds-1;
pK := @ctx.RK[ctx.Rounds];
asm
{AES_XorBlock(BI, ctx.RK[ctx.Rounds], s);}
db $66; pusha
les si,[BI]
db $66; mov ax,es:[si]
db $66; mov bx,es:[si+4]
db $66; mov cx,es:[si+8]
db $66; mov dx,es:[si+12]
les di,[pK]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
db $66; mov word ptr [s],ax
db $66; mov word ptr [s+4],bx
db $66; mov word ptr [s+8],cx
db $66; mov word ptr [s+12],dx
sub di,16 {di -> ctx.RK[r]}
mov cx,[r]
{ *Note* in the following round loop }
{ op eax, mem[8*ebx] is calculated as }
{ lea esi, [edx+8*ebx] $66,$67,$8D,$34,$DA }
{ op eax, mem[esi] }
db $66; sub bx,bx {clear ebx}
db $66; sub dx,dx {clear edx}
@@1:
{TWA4(t)[3] := Td0[s[3*4+0]] xor Td1[s[2*4+1]] xor Td2[s[1*4+2]] xor Td3[s[0*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr s[3*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr s[2*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr s[1*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr s[0*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di+12]
db $66; mov word ptr t[12],ax
{TWA4(t)[2] := Td0[s[2*4+0]] xor Td1[s[1*4+1]] xor Td2[s[0*4+2]] xor Td3[s[3*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr s[2*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr s[1*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr s[0*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr s[3*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di+8]
db $66; mov word ptr t[8],ax
{TWA4(t)[1] := Td0[s[1*4+0]] xor Td1[s[0*4+1]] xor Td2[s[3*4+2]] xor Td3[s[2*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr s[1*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr s[0*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr s[3*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr s[2*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di+4]
db $66; mov word ptr t[4],ax
{TWA4(t)[0] := Td0[s[0*4+0]] xor Td1[s[3*4+1]] xor Td2[s[2*4+2]] xor Td3[s[1*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr s[0*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr s[3*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr s[2*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr s[1*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di]
db $66; mov word ptr t[0],ax
{ dec(r); if r<1 then break;}
sub cx,1
jle @@2
{TWA4(s)[3] := Td0[t[3*4+0]] xor Td1[t[2*4+1]] xor Td2[t[1*4+2]] xor Td3[t[0*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr t[3*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr t[2*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr t[1*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr t[0*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di-4]
db $66; mov word ptr s[12],ax
{TWA4(s)[2] := Td0[t[2*4+0]] xor Td1[t[1*4+1]] xor Td2[t[0*4+2]] xor Td3[t[3*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr t[2*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr t[1*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr t[0*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr t[3*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di-8]
db $66; mov word ptr s[8],ax
{TWA4(s)[1] := Td0[t[1*4+0]] xor Td1[t[0*4+1]] xor Td2[t[3*4+2]] xor Td3[t[2*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr t[1*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr t[0*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr t[3*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr t[2*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di-12]
db $66; mov word ptr s[4],ax
{TWA4(s)[0] := Td0[t[0*4+0]] xor Td1[t[3*4+1]] xor Td2[t[2*4+2]] xor Td3[t[1*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr t[0*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCd[si+3]
mov bl,byte ptr t[3*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+2]
mov bl,byte ptr t[2*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si+1]
mov bl,byte ptr t[1*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCd[si]
db $66; xor ax,es:[di-16]
db $66; mov word ptr s[0],ax
sub di,32
dec cx
jmp @@1
@@2: sub di,16 {di -> ctx.RK[0]}
sub bx,bx
mov bl, byte ptr t[0*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[0],al
mov bl, byte ptr t[3*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[1],al
mov bl, byte ptr t[2*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[2],al
mov bl, byte ptr t[1*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[3],al
mov bl, byte ptr t[1*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[4],al
mov bl, byte ptr t[0*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[5],al
mov bl, byte ptr t[3*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[6],al
mov bl, byte ptr t[2*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[7],al
mov bl, byte ptr t[2*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[8],al
mov bl, byte ptr t[1*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[9],al
mov bl, byte ptr t[0*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[10],al
mov bl, byte ptr t[3*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[11],al
mov bl, byte ptr t[3*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[12],al
mov bl, byte ptr t[2*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[13],al
mov bl, byte ptr t[1*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[14],al
mov bl, byte ptr t[0*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr Tcd[bx+7]
mov byte ptr s[15],al
{AES_XorBlock(s, ctx.RK[0], BO);}
db $66; mov ax,word ptr [s]
db $66; mov bx,word ptr [s+4]
db $66; mov cx,word ptr [s+8]
db $66; mov dx,word ptr [s+12]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
les si,[BO]
db $66; mov es:[si],ax
db $66; mov es:[si+4],bx
db $66; mov es:[si+8],cx
db $66; mov es:[si+12],dx
db $66; popa
end;
end;
{---------------------------------------------------------------------------}
procedure MakeDecrKey(var ctx: TAESContext);
{-Calculate decryption key from encryption key}
var
n: integer;
p: PLong;
begin
p := Plong(@ctx.RK[1]);
n := 4*(ctx.Rounds-1);
{BASM version of 16 bit code, no need for local x/t}
{implicit endian conversion compared with [2]}
asm
les si,[p]
mov cx,[n]
@@1: mov dx,es:[si]
sub bh,bh
mov bl,dl
mov bl,byte ptr SBox[bx]
shl bx,3
db $66; mov ax,word ptr TCd[bx+3]
sub bh,bh
mov bl,dh
mov bl,byte ptr SBox[bx]
shl bx,3
db $66; xor ax,word ptr TCd[bx+2]
mov dx,es:[si+2]
sub bh,bh
mov bl,dl
mov bl,byte ptr SBox[bx]
shl bx,3
db $66; xor ax,word ptr TCd[bx+1]
sub bh,bh
mov bl,dh
mov bl,byte ptr SBox[bx]
shl bx,3
db $66; xor ax,word ptr TCd[bx]
db $66; mov es:[si],ax
add si,4
dec cx
jnz @@1
end;
end;

View File

@ -0,0 +1,197 @@
(*************************************************************************
Include file for AES_DECR.PAS - Compressed tables/Helper types
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version for compressed tables
0.11 13.07.06 we Removed InvSBox, b3 gets box byte
0.12 19.07.06 we TCdDummy
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
type
TH3 = packed record
L: longint;
b0,b1,b2,box: byte;
end;
TH2 = packed record
b0: byte;
L: longint;
b1,b2,box: byte;
end;
TH1 = packed record
b0,b1: byte;
L: longint;
b2,box: byte;
end;
TH0 = packed record
b0,b1,b2: byte;
L: longint;
box: byte;
end;
TDU = record
case integer of
0: (D0: TH0);
1: (D1: TH1);
2: (D2: TH2);
3: (D3: TH3);
end;
{$ifdef StrictLong}
{$warnings off}
{$R-} {avoid D9+ errors!}
{$endif}
const
{$ifdef AES_Decr_DummyAlign}
TCdDummy : longint = 0; {Use to align TCd to 8 byte boundary}
{$endif}
TCd: packed array[0..2047] of byte = (
$f4,$a7,$50,$51,$f4,$a7,$50,$52,$41,$65,$53,$7e,$41,$65,$53,$09,
$17,$a4,$c3,$1a,$17,$a4,$c3,$6a,$27,$5e,$96,$3a,$27,$5e,$96,$d5,
$ab,$6b,$cb,$3b,$ab,$6b,$cb,$30,$9d,$45,$f1,$1f,$9d,$45,$f1,$36,
$fa,$58,$ab,$ac,$fa,$58,$ab,$a5,$e3,$03,$93,$4b,$e3,$03,$93,$38,
$30,$fa,$55,$20,$30,$fa,$55,$bf,$76,$6d,$f6,$ad,$76,$6d,$f6,$40,
$cc,$76,$91,$88,$cc,$76,$91,$a3,$02,$4c,$25,$f5,$02,$4c,$25,$9e,
$e5,$d7,$fc,$4f,$e5,$d7,$fc,$81,$2a,$cb,$d7,$c5,$2a,$cb,$d7,$f3,
$35,$44,$80,$26,$35,$44,$80,$d7,$62,$a3,$8f,$b5,$62,$a3,$8f,$fb,
$b1,$5a,$49,$de,$b1,$5a,$49,$7c,$ba,$1b,$67,$25,$ba,$1b,$67,$e3,
$ea,$0e,$98,$45,$ea,$0e,$98,$39,$fe,$c0,$e1,$5d,$fe,$c0,$e1,$82,
$2f,$75,$02,$c3,$2f,$75,$02,$9b,$4c,$f0,$12,$81,$4c,$f0,$12,$2f,
$46,$97,$a3,$8d,$46,$97,$a3,$ff,$d3,$f9,$c6,$6b,$d3,$f9,$c6,$87,
$8f,$5f,$e7,$03,$8f,$5f,$e7,$34,$92,$9c,$95,$15,$92,$9c,$95,$8e,
$6d,$7a,$eb,$bf,$6d,$7a,$eb,$43,$52,$59,$da,$95,$52,$59,$da,$44,
$be,$83,$2d,$d4,$be,$83,$2d,$c4,$74,$21,$d3,$58,$74,$21,$d3,$de,
$e0,$69,$29,$49,$e0,$69,$29,$e9,$c9,$c8,$44,$8e,$c9,$c8,$44,$cb,
$c2,$89,$6a,$75,$c2,$89,$6a,$54,$8e,$79,$78,$f4,$8e,$79,$78,$7b,
$58,$3e,$6b,$99,$58,$3e,$6b,$94,$b9,$71,$dd,$27,$b9,$71,$dd,$32,
$e1,$4f,$b6,$be,$e1,$4f,$b6,$a6,$88,$ad,$17,$f0,$88,$ad,$17,$c2,
$20,$ac,$66,$c9,$20,$ac,$66,$23,$ce,$3a,$b4,$7d,$ce,$3a,$b4,$3d,
$df,$4a,$18,$63,$df,$4a,$18,$ee,$1a,$31,$82,$e5,$1a,$31,$82,$4c,
$51,$33,$60,$97,$51,$33,$60,$95,$53,$7f,$45,$62,$53,$7f,$45,$0b,
$64,$77,$e0,$b1,$64,$77,$e0,$42,$6b,$ae,$84,$bb,$6b,$ae,$84,$fa,
$81,$a0,$1c,$fe,$81,$a0,$1c,$c3,$08,$2b,$94,$f9,$08,$2b,$94,$4e,
$48,$68,$58,$70,$48,$68,$58,$08,$45,$fd,$19,$8f,$45,$fd,$19,$2e,
$de,$6c,$87,$94,$de,$6c,$87,$a1,$7b,$f8,$b7,$52,$7b,$f8,$b7,$66,
$73,$d3,$23,$ab,$73,$d3,$23,$28,$4b,$02,$e2,$72,$4b,$02,$e2,$d9,
$1f,$8f,$57,$e3,$1f,$8f,$57,$24,$55,$ab,$2a,$66,$55,$ab,$2a,$b2,
$eb,$28,$07,$b2,$eb,$28,$07,$76,$b5,$c2,$03,$2f,$b5,$c2,$03,$5b,
$c5,$7b,$9a,$86,$c5,$7b,$9a,$a2,$37,$08,$a5,$d3,$37,$08,$a5,$49,
$28,$87,$f2,$30,$28,$87,$f2,$6d,$bf,$a5,$b2,$23,$bf,$a5,$b2,$8b,
$03,$6a,$ba,$02,$03,$6a,$ba,$d1,$16,$82,$5c,$ed,$16,$82,$5c,$25,
$cf,$1c,$2b,$8a,$cf,$1c,$2b,$72,$79,$b4,$92,$a7,$79,$b4,$92,$f8,
$07,$f2,$f0,$f3,$07,$f2,$f0,$f6,$69,$e2,$a1,$4e,$69,$e2,$a1,$64,
$da,$f4,$cd,$65,$da,$f4,$cd,$86,$05,$be,$d5,$06,$05,$be,$d5,$68,
$34,$62,$1f,$d1,$34,$62,$1f,$98,$a6,$fe,$8a,$c4,$a6,$fe,$8a,$16,
$2e,$53,$9d,$34,$2e,$53,$9d,$d4,$f3,$55,$a0,$a2,$f3,$55,$a0,$a4,
$8a,$e1,$32,$05,$8a,$e1,$32,$5c,$f6,$eb,$75,$a4,$f6,$eb,$75,$cc,
$83,$ec,$39,$0b,$83,$ec,$39,$5d,$60,$ef,$aa,$40,$60,$ef,$aa,$65,
$71,$9f,$06,$5e,$71,$9f,$06,$b6,$6e,$10,$51,$bd,$6e,$10,$51,$92,
$21,$8a,$f9,$3e,$21,$8a,$f9,$6c,$dd,$06,$3d,$96,$dd,$06,$3d,$70,
$3e,$05,$ae,$dd,$3e,$05,$ae,$48,$e6,$bd,$46,$4d,$e6,$bd,$46,$50,
$54,$8d,$b5,$91,$54,$8d,$b5,$fd,$c4,$5d,$05,$71,$c4,$5d,$05,$ed,
$06,$d4,$6f,$04,$06,$d4,$6f,$b9,$50,$15,$ff,$60,$50,$15,$ff,$da,
$98,$fb,$24,$19,$98,$fb,$24,$5e,$bd,$e9,$97,$d6,$bd,$e9,$97,$15,
$40,$43,$cc,$89,$40,$43,$cc,$46,$d9,$9e,$77,$67,$d9,$9e,$77,$57,
$e8,$42,$bd,$b0,$e8,$42,$bd,$a7,$89,$8b,$88,$07,$89,$8b,$88,$8d,
$19,$5b,$38,$e7,$19,$5b,$38,$9d,$c8,$ee,$db,$79,$c8,$ee,$db,$84,
$7c,$0a,$47,$a1,$7c,$0a,$47,$90,$42,$0f,$e9,$7c,$42,$0f,$e9,$d8,
$84,$1e,$c9,$f8,$84,$1e,$c9,$ab,$00,$00,$00,$00,$00,$00,$00,$00,
$80,$86,$83,$09,$80,$86,$83,$8c,$2b,$ed,$48,$32,$2b,$ed,$48,$bc,
$11,$70,$ac,$1e,$11,$70,$ac,$d3,$5a,$72,$4e,$6c,$5a,$72,$4e,$0a,
$0e,$ff,$fb,$fd,$0e,$ff,$fb,$f7,$85,$38,$56,$0f,$85,$38,$56,$e4,
$ae,$d5,$1e,$3d,$ae,$d5,$1e,$58,$2d,$39,$27,$36,$2d,$39,$27,$05,
$0f,$d9,$64,$0a,$0f,$d9,$64,$b8,$5c,$a6,$21,$68,$5c,$a6,$21,$b3,
$5b,$54,$d1,$9b,$5b,$54,$d1,$45,$36,$2e,$3a,$24,$36,$2e,$3a,$06,
$0a,$67,$b1,$0c,$0a,$67,$b1,$d0,$57,$e7,$0f,$93,$57,$e7,$0f,$2c,
$ee,$96,$d2,$b4,$ee,$96,$d2,$1e,$9b,$91,$9e,$1b,$9b,$91,$9e,$8f,
$c0,$c5,$4f,$80,$c0,$c5,$4f,$ca,$dc,$20,$a2,$61,$dc,$20,$a2,$3f,
$77,$4b,$69,$5a,$77,$4b,$69,$0f,$12,$1a,$16,$1c,$12,$1a,$16,$02,
$93,$ba,$0a,$e2,$93,$ba,$0a,$c1,$a0,$2a,$e5,$c0,$a0,$2a,$e5,$af,
$22,$e0,$43,$3c,$22,$e0,$43,$bd,$1b,$17,$1d,$12,$1b,$17,$1d,$03,
$09,$0d,$0b,$0e,$09,$0d,$0b,$01,$8b,$c7,$ad,$f2,$8b,$c7,$ad,$13,
$b6,$a8,$b9,$2d,$b6,$a8,$b9,$8a,$1e,$a9,$c8,$14,$1e,$a9,$c8,$6b,
$f1,$19,$85,$57,$f1,$19,$85,$3a,$75,$07,$4c,$af,$75,$07,$4c,$91,
$99,$dd,$bb,$ee,$99,$dd,$bb,$11,$7f,$60,$fd,$a3,$7f,$60,$fd,$41,
$01,$26,$9f,$f7,$01,$26,$9f,$4f,$72,$f5,$bc,$5c,$72,$f5,$bc,$67,
$66,$3b,$c5,$44,$66,$3b,$c5,$dc,$fb,$7e,$34,$5b,$fb,$7e,$34,$ea,
$43,$29,$76,$8b,$43,$29,$76,$97,$23,$c6,$dc,$cb,$23,$c6,$dc,$f2,
$ed,$fc,$68,$b6,$ed,$fc,$68,$cf,$e4,$f1,$63,$b8,$e4,$f1,$63,$ce,
$31,$dc,$ca,$d7,$31,$dc,$ca,$f0,$63,$85,$10,$42,$63,$85,$10,$b4,
$97,$22,$40,$13,$97,$22,$40,$e6,$c6,$11,$20,$84,$c6,$11,$20,$73,
$4a,$24,$7d,$85,$4a,$24,$7d,$96,$bb,$3d,$f8,$d2,$bb,$3d,$f8,$ac,
$f9,$32,$11,$ae,$f9,$32,$11,$74,$29,$a1,$6d,$c7,$29,$a1,$6d,$22,
$9e,$2f,$4b,$1d,$9e,$2f,$4b,$e7,$b2,$30,$f3,$dc,$b2,$30,$f3,$ad,
$86,$52,$ec,$0d,$86,$52,$ec,$35,$c1,$e3,$d0,$77,$c1,$e3,$d0,$85,
$b3,$16,$6c,$2b,$b3,$16,$6c,$e2,$70,$b9,$99,$a9,$70,$b9,$99,$f9,
$94,$48,$fa,$11,$94,$48,$fa,$37,$e9,$64,$22,$47,$e9,$64,$22,$e8,
$fc,$8c,$c4,$a8,$fc,$8c,$c4,$1c,$f0,$3f,$1a,$a0,$f0,$3f,$1a,$75,
$7d,$2c,$d8,$56,$7d,$2c,$d8,$df,$33,$90,$ef,$22,$33,$90,$ef,$6e,
$49,$4e,$c7,$87,$49,$4e,$c7,$47,$38,$d1,$c1,$d9,$38,$d1,$c1,$f1,
$ca,$a2,$fe,$8c,$ca,$a2,$fe,$1a,$d4,$0b,$36,$98,$d4,$0b,$36,$71,
$f5,$81,$cf,$a6,$f5,$81,$cf,$1d,$7a,$de,$28,$a5,$7a,$de,$28,$29,
$b7,$8e,$26,$da,$b7,$8e,$26,$c5,$ad,$bf,$a4,$3f,$ad,$bf,$a4,$89,
$3a,$9d,$e4,$2c,$3a,$9d,$e4,$6f,$78,$92,$0d,$50,$78,$92,$0d,$b7,
$5f,$cc,$9b,$6a,$5f,$cc,$9b,$62,$7e,$46,$62,$54,$7e,$46,$62,$0e,
$8d,$13,$c2,$f6,$8d,$13,$c2,$aa,$d8,$b8,$e8,$90,$d8,$b8,$e8,$18,
$39,$f7,$5e,$2e,$39,$f7,$5e,$be,$c3,$af,$f5,$82,$c3,$af,$f5,$1b,
$5d,$80,$be,$9f,$5d,$80,$be,$fc,$d0,$93,$7c,$69,$d0,$93,$7c,$56,
$d5,$2d,$a9,$6f,$d5,$2d,$a9,$3e,$25,$12,$b3,$cf,$25,$12,$b3,$4b,
$ac,$99,$3b,$c8,$ac,$99,$3b,$c6,$18,$7d,$a7,$10,$18,$7d,$a7,$d2,
$9c,$63,$6e,$e8,$9c,$63,$6e,$79,$3b,$bb,$7b,$db,$3b,$bb,$7b,$20,
$26,$78,$09,$cd,$26,$78,$09,$9a,$59,$18,$f4,$6e,$59,$18,$f4,$db,
$9a,$b7,$01,$ec,$9a,$b7,$01,$c0,$4f,$9a,$a8,$83,$4f,$9a,$a8,$fe,
$95,$6e,$65,$e6,$95,$6e,$65,$78,$ff,$e6,$7e,$aa,$ff,$e6,$7e,$cd,
$bc,$cf,$08,$21,$bc,$cf,$08,$5a,$15,$e8,$e6,$ef,$15,$e8,$e6,$f4,
$e7,$9b,$d9,$ba,$e7,$9b,$d9,$1f,$6f,$36,$ce,$4a,$6f,$36,$ce,$dd,
$9f,$09,$d4,$ea,$9f,$09,$d4,$a8,$b0,$7c,$d6,$29,$b0,$7c,$d6,$33,
$a4,$b2,$af,$31,$a4,$b2,$af,$88,$3f,$23,$31,$2a,$3f,$23,$31,$07,
$a5,$94,$30,$c6,$a5,$94,$30,$c7,$a2,$66,$c0,$35,$a2,$66,$c0,$31,
$4e,$bc,$37,$74,$4e,$bc,$37,$b1,$82,$ca,$a6,$fc,$82,$ca,$a6,$12,
$90,$d0,$b0,$e0,$90,$d0,$b0,$10,$a7,$d8,$15,$33,$a7,$d8,$15,$59,
$04,$98,$4a,$f1,$04,$98,$4a,$27,$ec,$da,$f7,$41,$ec,$da,$f7,$80,
$cd,$50,$0e,$7f,$cd,$50,$0e,$ec,$91,$f6,$2f,$17,$91,$f6,$2f,$5f,
$4d,$d6,$8d,$76,$4d,$d6,$8d,$60,$ef,$b0,$4d,$43,$ef,$b0,$4d,$51,
$aa,$4d,$54,$cc,$aa,$4d,$54,$7f,$96,$04,$df,$e4,$96,$04,$df,$a9,
$d1,$b5,$e3,$9e,$d1,$b5,$e3,$19,$6a,$88,$1b,$4c,$6a,$88,$1b,$b5,
$2c,$1f,$b8,$c1,$2c,$1f,$b8,$4a,$65,$51,$7f,$46,$65,$51,$7f,$0d,
$5e,$ea,$04,$9d,$5e,$ea,$04,$2d,$8c,$35,$5d,$01,$8c,$35,$5d,$e5,
$87,$74,$73,$fa,$87,$74,$73,$7a,$0b,$41,$2e,$fb,$0b,$41,$2e,$9f,
$67,$1d,$5a,$b3,$67,$1d,$5a,$93,$db,$d2,$52,$92,$db,$d2,$52,$c9,
$10,$56,$33,$e9,$10,$56,$33,$9c,$d6,$47,$13,$6d,$d6,$47,$13,$ef,
$d7,$61,$8c,$9a,$d7,$61,$8c,$a0,$a1,$0c,$7a,$37,$a1,$0c,$7a,$e0,
$f8,$14,$8e,$59,$f8,$14,$8e,$3b,$13,$3c,$89,$eb,$13,$3c,$89,$4d,
$a9,$27,$ee,$ce,$a9,$27,$ee,$ae,$61,$c9,$35,$b7,$61,$c9,$35,$2a,
$1c,$e5,$ed,$e1,$1c,$e5,$ed,$f5,$47,$b1,$3c,$7a,$47,$b1,$3c,$b0,
$d2,$df,$59,$9c,$d2,$df,$59,$c8,$f2,$73,$3f,$55,$f2,$73,$3f,$eb,
$14,$ce,$79,$18,$14,$ce,$79,$bb,$c7,$37,$bf,$73,$c7,$37,$bf,$3c,
$f7,$cd,$ea,$53,$f7,$cd,$ea,$83,$fd,$aa,$5b,$5f,$fd,$aa,$5b,$53,
$3d,$6f,$14,$df,$3d,$6f,$14,$99,$44,$db,$86,$78,$44,$db,$86,$61,
$af,$f3,$81,$ca,$af,$f3,$81,$17,$68,$c4,$3e,$b9,$68,$c4,$3e,$2b,
$24,$34,$2c,$38,$24,$34,$2c,$04,$a3,$40,$5f,$c2,$a3,$40,$5f,$7e,
$1d,$c3,$72,$16,$1d,$c3,$72,$ba,$e2,$25,$0c,$bc,$e2,$25,$0c,$77,
$3c,$49,$8b,$28,$3c,$49,$8b,$d6,$0d,$95,$41,$ff,$0d,$95,$41,$26,
$a8,$01,$71,$39,$a8,$01,$71,$e1,$0c,$b3,$de,$08,$0c,$b3,$de,$69,
$b4,$e4,$9c,$d8,$b4,$e4,$9c,$14,$56,$c1,$90,$64,$56,$c1,$90,$63,
$cb,$84,$61,$7b,$cb,$84,$61,$55,$32,$b6,$70,$d5,$32,$b6,$70,$21,
$6c,$5c,$74,$48,$6c,$5c,$74,$0c,$b8,$57,$42,$d0,$b8,$57,$42,$7d);
var
Td: packed array[byte] of TDU absolute TCd;
{$ifdef StrictLong}
{$warnings on}
{$ifdef RangeChecks_on}
{$R+}
{$endif}
{$endif}

View File

@ -0,0 +1,94 @@
(*************************************************************************
Include file for AES_DECR.PAS - AES_Decrypt for Pascal16/Compressed tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version for compressed tables
0.11 13.07.06 we Uses TDe box byte instead of InvSBox
0.12 15.11.08 we Use Ptr2Inc from BTypes
**************************************************************************)
(**** (C) Copyright 2002-2008 Wolfgang Ehrhardt -- see copying_we.txt ****)
{Normally used for TP5/5.5 (and during development BP7)}
{---------------------------------------------------------------------------}
procedure AES_Decrypt(var ctx: TAESContext; {$ifdef CONST} const {$else} var {$endif} BI: TAESBlock; var BO: TAESBlock);
{-decrypt one block (in ECB mode)}
label done;
var
r: integer;
pK: PWA4; {pointer to loop rount key }
s,t: TAESBlock;
begin
{Setup key pointer}
pK := PWA4(@ctx.RK[ctx.Rounds]);
{Initialize with input block}
TWA4(s)[0] := TWA4(BI)[0] xor pK^[0];
TWA4(s)[1] := TWA4(BI)[1] xor pK^[1];
TWA4(s)[2] := TWA4(BI)[2] xor pK^[2];
TWA4(s)[3] := TWA4(BI)[3] xor pK^[3];
dec(Ptr2Inc(pK), 4*sizeof(longint));
r := ctx.Rounds-1;
while true do begin
TWA4(t)[3] := Td[s[3*4+0]].D0.L xor Td[s[2*4+1]].D1.L xor Td[s[1*4+2]].D2.L xor Td[s[0*4+3]].D3.L xor pK^[3];
TWA4(t)[2] := Td[s[2*4+0]].D0.L xor Td[s[1*4+1]].D1.L xor Td[s[0*4+2]].D2.L xor Td[s[3*4+3]].D3.L xor pK^[2];
TWA4(t)[1] := Td[s[1*4+0]].D0.L xor Td[s[0*4+1]].D1.L xor Td[s[3*4+2]].D2.L xor Td[s[2*4+3]].D3.L xor pK^[1];
TWA4(t)[0] := Td[s[0*4+0]].D0.L xor Td[s[3*4+1]].D1.L xor Td[s[2*4+2]].D2.L xor Td[s[1*4+3]].D3.L xor pK^[0];
dec(Ptr2Inc(pK), 4*sizeof(longint));
dec(r);
if r<1 then goto done;
TWA4(s)[3] := Td[t[3*4+0]].D0.L xor Td[t[2*4+1]].D1.L xor Td[t[1*4+2]].D2.L xor Td[t[0*4+3]].D3.L xor pK^[3];
TWA4(s)[2] := Td[t[2*4+0]].D0.L xor Td[t[1*4+1]].D1.L xor Td[t[0*4+2]].D2.L xor Td[t[3*4+3]].D3.L xor pK^[2];
TWA4(s)[1] := Td[t[1*4+0]].D0.L xor Td[t[0*4+1]].D1.L xor Td[t[3*4+2]].D2.L xor Td[t[2*4+3]].D3.L xor pK^[1];
TWA4(s)[0] := Td[t[0*4+0]].D0.L xor Td[t[3*4+1]].D1.L xor Td[t[2*4+2]].D2.L xor Td[t[1*4+3]].D3.L xor pK^[0];
dec(Ptr2Inc(pK), 4*sizeof(longint));
dec(r);
end;
done:
s[00] := Td[t[0*4+0]].D0.box;
s[01] := Td[t[3*4+1]].D0.box;
s[02] := Td[t[2*4+2]].D0.box;
s[03] := Td[t[1*4+3]].D0.box;
s[04] := Td[t[1*4+0]].D0.box;
s[05] := Td[t[0*4+1]].D0.box;
s[06] := Td[t[3*4+2]].D0.box;
s[07] := Td[t[2*4+3]].D0.box;
s[08] := Td[t[2*4+0]].D0.box;
s[09] := Td[t[1*4+1]].D0.box;
s[10] := Td[t[0*4+2]].D0.box;
s[11] := Td[t[3*4+3]].D0.box;
s[12] := Td[t[3*4+0]].D0.box;
s[13] := Td[t[2*4+1]].D0.box;
s[14] := Td[t[1*4+2]].D0.box;
s[15] := Td[t[0*4+3]].D0.box;
TWA4(BO)[0] := TWA4(s)[0] xor pK^[0];
TWA4(BO)[1] := TWA4(s)[1] xor pK^[1];
TWA4(BO)[2] := TWA4(s)[2] xor pK^[2];
TWA4(BO)[3] := TWA4(s)[3] xor pK^[3];
end;
{---------------------------------------------------------------------------}
procedure MakeDecrKey(var ctx: TAESContext);
{-Calculate decryption key from encryption key}
var
i: integer;
x: longint;
t: TBA4 absolute x;
begin
with ctx do begin
for i:=4 to 4*Rounds-1 do begin
{Inverse MixColumns transformation: use Sbox and}
{implicit endian conversion compared with [2] }
x := TAWK(RK)[i];
TAWK(RK)[i] := Td[SBox[t[3]]].D3.L xor Td[SBox[t[2]]].D2.L xor Td[SBox[t[1]]].D1.L xor Td[SBox[t[0]]].D0.L;
end;
end;
end;

View File

@ -0,0 +1,83 @@
(*************************************************************************
Include file for AES_DECR.PAS - AES_Decrypt for BIT32/Compressed tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version for compressed tables
0.11 09.07.06 we Removed AES_LONGBOX code
0.12 13.07.06 we Uses TCd box byte instead of InvSBox
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{---------------------------------------------------------------------------}
procedure AES_Decrypt(var ctx: TAESContext; const BI: TAESBlock; var BO: TAESBlock);
{-decrypt one block (in ECB mode)}
var
r: integer; {round loop countdown counter}
pK: PWA4; {pointer to loop rount key }
s0,s1,s2,s3: longint; {TAESBlock s as separate variables}
t: TWA4;
begin
{Setup key pointer}
pK := PWA4(@ctx.RK[ctx.Rounds]);
{Initialize with input block}
s0 := TWA4(BI)[0] xor pK^[0];
s1 := TWA4(BI)[1] xor pK^[1];
s2 := TWA4(BI)[2] xor pK^[2];
s3 := TWA4(BI)[3] xor pK^[3];
dec(pK);
{perform encryption rounds}
for r:=1 to ctx.Rounds-1 do begin
t[3] := Td[s3 and $ff].D0.L xor Td[s2 shr 8 and $ff].D1.L xor Td[s1 shr 16 and $ff].D2.L xor Td[s0 shr 24].D3.L xor pK^[3];
t[2] := Td[s2 and $ff].D0.L xor Td[s1 shr 8 and $ff].D1.L xor Td[s0 shr 16 and $ff].D2.L xor Td[s3 shr 24].D3.L xor pK^[2];
t[1] := Td[s1 and $ff].D0.L xor Td[s0 shr 8 and $ff].D1.L xor Td[s3 shr 16 and $ff].D2.L xor Td[s2 shr 24].D3.L xor pK^[1];
s0 := Td[s0 and $ff].D0.L xor Td[s3 shr 8 and $ff].D1.L xor Td[s2 shr 16 and $ff].D2.L xor Td[s1 shr 24].D3.L xor pK^[0];
s1 := t[1];
s2 := t[2];
s3 := t[3];
dec(pK);
end;
{Uses InvSbox byte from Td and shl, needs type cast longint() for 16 bit compilers}
TWA4(BO)[0] := (longint(Td[s0 and $ff].D0.box) xor
longint(Td[s3 shr 8 and $ff].D0.box) shl 8 xor
longint(Td[s2 shr 16 and $ff].D0.box) shl 16 xor
longint(Td[s1 shr 24 ].D0.box) shl 24 ) xor pK^[0];
TWA4(BO)[1] := (longint(Td[s1 and $ff].D0.box) xor
longint(Td[s0 shr 8 and $ff].D0.box) shl 8 xor
longint(Td[s3 shr 16 and $ff].D0.box) shl 16 xor
longint(Td[s2 shr 24 ].D0.box) shl 24 ) xor pK^[1];
TWA4(BO)[2] := (longint(Td[s2 and $ff ].D0.box) xor
longint(Td[s1 shr 8 and $ff].D0.box) shl 8 xor
longint(Td[s0 shr 16 and $ff].D0.box) shl 16 xor
longint(Td[s3 shr 24 ].D0.box) shl 24 ) xor pK^[2];
TWA4(BO)[3] := (longint(Td[s3 and $ff ].D0.box) xor
longint(Td[s2 shr 8 and $ff].D0.box) shl 8 xor
longint(Td[s1 shr 16 and $ff].D0.box) shl 16 xor
longint(Td[s0 shr 24 ].D0.box) shl 24 ) xor pK^[3];
end;
{---------------------------------------------------------------------------}
procedure MakeDecrKey(var ctx: TAESContext);
{-Calculate decryption key from encryption key}
var
i: integer;
p: PLong;
x: longint;
begin
p := PLong(@ctx.RK[1]);
for i:=1 to 4*(ctx.Rounds-1) do begin
x := p^;
p^ := Td[SBox[x shr 24]].D3.L xor Td[SBox[x shr 16 and $ff]].D2.L xor
Td[SBox[x shr 8 and $ff]].D1.L xor Td[SBox[x and $ff]].D0.L;
inc(p);
end;
end;

View File

@ -0,0 +1,358 @@
(*************************************************************************
Include file for AES_DECR.PAS - AES_Decrypt for BASM16/Full tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version from AES_DECR.PAS
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{16 bit BASM used for TP6, BP7, Delphi1}
{---------------------------------------------------------------------------}
procedure AES_Decrypt(var ctx: TAESContext; {$ifdef CONST} const {$else} var {$endif} BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
var
s,t: TAESBlock;
r: integer;
pK: pointer;
begin
r := ctx.Rounds-1;
pK := @ctx.RK[ctx.Rounds];
asm
{AES_XorBlock(BI, ctx.RK[ctx.Rounds], s);}
db $66; pusha
les si,[BI]
db $66; mov ax,es:[si]
db $66; mov bx,es:[si+4]
db $66; mov cx,es:[si+8]
db $66; mov dx,es:[si+12]
les di,[pK]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
db $66; mov word ptr [s],ax
db $66; mov word ptr [s+4],bx
db $66; mov word ptr [s+8],cx
db $66; mov word ptr [s+12],dx
sub di,16 {di -> ctx.RK[r]}
mov cx,[r]
{ op eax, mem[4*bx] is calculated as }
{ lea esi, [ebx + 2*ebx] }
{ op eax, mem[ebx+esi] }
{ lea esi,[ebx+2*ebx] = db $66,$67,$8D,$34,$5B; }
db $66; sub bx,bx
@@1:
{TWA4(t)[3] := Td0[s[3*4+0]] xor Td1[s[2*4+1]] xor Td2[s[1*4+2]] xor Td3[s[0*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr s[3*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Td0[bx+si]
mov bl,byte ptr s[2*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td1[bx+si]
mov bl,byte ptr s[1*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td2[bx+si]
mov bl,byte ptr s[0*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td3[bx+si]
db $66; xor ax,es:[di+12]
db $66; mov word ptr t[12],ax
{TWA4(t)[2] := Td0[s[2*4+0]] xor Td1[s[1*4+1]] xor Td2[s[0*4+2]] xor Td3[s[3*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr s[2*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Td0[bx+si]
mov bl,byte ptr s[1*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td1[bx+si]
mov bl,byte ptr s[0*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td2[bx+si]
mov bl,byte ptr s[3*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td3[bx+si]
db $66; xor ax,es:[di+8]
db $66; mov word ptr t[8],ax
{TWA4(t)[1] := Td0[s[1*4+0]] xor Td1[s[0*4+1]] xor Td2[s[3*4+2]] xor Td3[s[2*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr s[1*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Td0[bx+si]
mov bl,byte ptr s[0*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td1[bx+si]
mov bl,byte ptr s[3*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td2[bx+si]
mov bl,byte ptr s[2*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td3[bx+si]
db $66; xor ax,es:[di+4]
db $66; mov word ptr t[4],ax
{TWA4(t)[0] := Td0[s[0*4+0]] xor Td1[s[3*4+1]] xor Td2[s[2*4+2]] xor Td3[s[1*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr s[0*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Td0[bx+si]
mov bl,byte ptr s[3*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td1[bx+si]
mov bl,byte ptr s[2*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td2[bx+si]
mov bl,byte ptr s[1*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td3[bx+si]
db $66; xor ax,es:[di]
db $66; mov word ptr t[0],ax
{ dec(r); if r<1 then break;}
sub cx,1
jle @@2
{TWA4(s)[3] := Td0[t[3*4+0]] xor Td1[t[2*4+1]] xor Td2[t[1*4+2]] xor Td3[t[0*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr t[3*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Td0[bx+si]
mov bl,byte ptr t[2*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td1[bx+si]
mov bl,byte ptr t[1*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td2[bx+si]
mov bl,byte ptr t[0*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td3[bx+si]
db $66; xor ax,es:[di-4]
db $66; mov word ptr s[12],ax
{TWA4(s)[2] := Td0[t[2*4+0]] xor Td1[t[1*4+1]] xor Td2[t[0*4+2]] xor Td3[t[3*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr t[2*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Td0[bx+si]
mov bl,byte ptr t[1*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td1[bx+si]
mov bl,byte ptr t[0*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td2[bx+si]
mov bl,byte ptr t[3*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td3[bx+si]
db $66; xor ax,es:[di-8]
db $66; mov word ptr s[8],ax
{TWA4(s)[1] := Td0[t[1*4+0]] xor Td1[t[0*4+1]] xor Td2[t[3*4+2]] xor Td3[t[2*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr t[1*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Td0[bx+si]
mov bl,byte ptr t[0*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td1[bx+si]
mov bl,byte ptr t[3*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td2[bx+si]
mov bl,byte ptr t[2*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td3[bx+si]
db $66; xor ax,es:[di-12]
db $66; mov word ptr s[4],ax
{TWA4(s)[0] := Td0[t[0*4+0]] xor Td1[t[3*4+1]] xor Td2[t[2*4+2]] xor Td3[t[1*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr t[0*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Td0[bx+si]
mov bl,byte ptr t[3*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td1[bx+si]
mov bl,byte ptr t[2*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td2[bx+si]
mov bl,byte ptr t[1*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Td3[bx+si]
db $66; xor ax,es:[di-16]
db $66; mov word ptr s[0],ax
sub di,32
dec cx
jmp @@1
@@2: sub di,16 {di -> ctx.RK[0]}
sub bx,bx
mov bl, byte ptr t[0*4+0]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[0],al
mov bl, byte ptr t[3*4+1]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[1],al
mov bl, byte ptr t[2*4+2]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[2],al
mov bl, byte ptr t[1*4+3]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[3],al
mov bl, byte ptr t[1*4+0]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[4],al
mov bl, byte ptr t[0*4+1]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[5],al
mov bl, byte ptr t[3*4+2]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[6],al
mov bl, byte ptr t[2*4+3]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[7],al
mov bl, byte ptr t[2*4+0]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[8],al
mov bl, byte ptr t[1*4+1]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[9],al
mov bl, byte ptr t[0*4+2]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[10],al
mov bl, byte ptr t[3*4+3]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[11],al
mov bl, byte ptr t[3*4+0]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[12],al
mov bl, byte ptr t[2*4+1]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[13],al
mov bl, byte ptr t[1*4+2]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[14],al
mov bl, byte ptr t[0*4+3]
mov al, byte ptr InvSBox[bx]
mov byte ptr s[15],al
{AES_XorBlock(s, ctx.RK[0], BO);}
db $66; mov ax,word ptr [s]
db $66; mov bx,word ptr [s+4]
db $66; mov cx,word ptr [s+8]
db $66; mov dx,word ptr [s+12]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
les si,[BO]
db $66; mov es:[si],ax
db $66; mov es:[si+4],bx
db $66; mov es:[si+8],cx
db $66; mov es:[si+12],dx
db $66; popa
end;
end;
{---------------------------------------------------------------------------}
procedure MakeDecrKey(var ctx: TAESContext);
{-Calculate decryption key from encryption key}
var
n: integer;
p: PLong;
begin
p := Plong(@ctx.RK[1]);
n := 4*(ctx.Rounds-1);
{BASM version of 16 bit code, no need for local x/t}
{implicit endian conversion compared with [2]}
asm
les si,[p]
mov cx,[n]
@@1: mov dx,es:[si]
sub bh,bh
mov bl,dl
mov bl,byte ptr SBox[bx]
shl bx,2
db $66; mov ax,word ptr Td0[bx]
sub bh,bh
mov bl,dh
mov bl,byte ptr SBox[bx]
shl bx,2
db $66; xor ax,word ptr Td1[bx]
mov dx,es:[si+2]
sub bh,bh
mov bl,dl
mov bl,byte ptr SBox[bx]
shl bx,2
db $66; xor ax,word ptr Td2[bx]
sub bh,bh
mov bl,dh
mov bl,byte ptr SBox[bx]
shl bx,2
db $66; xor ax,word ptr Td3[bx]
db $66; mov es:[si],ax
add si,4
dec cx
jnz @@1
end;
end;

View File

@ -0,0 +1,224 @@
(*************************************************************************
Include file for AES_DECR.PAS - Full tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version from AES_DECR.PAS
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{$ifdef StrictLong}
{$warnings off}
{$R-} {avoid D9+ errors!}
{$endif}
const
InvSBox: array[byte] of byte =
($52, $09, $6a, $d5, $30, $36, $a5, $38, $bf, $40, $a3, $9e, $81, $f3, $d7, $fb,
$7c, $e3, $39, $82, $9b, $2f, $ff, $87, $34, $8e, $43, $44, $c4, $de, $e9, $cb,
$54, $7b, $94, $32, $a6, $c2, $23, $3d, $ee, $4c, $95, $0b, $42, $fa, $c3, $4e,
$08, $2e, $a1, $66, $28, $d9, $24, $b2, $76, $5b, $a2, $49, $6d, $8b, $d1, $25,
$72, $f8, $f6, $64, $86, $68, $98, $16, $d4, $a4, $5c, $cc, $5d, $65, $b6, $92,
$6c, $70, $48, $50, $fd, $ed, $b9, $da, $5e, $15, $46, $57, $a7, $8d, $9d, $84,
$90, $d8, $ab, $00, $8c, $bc, $d3, $0a, $f7, $e4, $58, $05, $b8, $b3, $45, $06,
$d0, $2c, $1e, $8f, $ca, $3f, $0f, $02, $c1, $af, $bd, $03, $01, $13, $8a, $6b,
$3a, $91, $11, $41, $4f, $67, $dc, $ea, $97, $f2, $cf, $ce, $f0, $b4, $e6, $73,
$96, $ac, $74, $22, $e7, $ad, $35, $85, $e2, $f9, $37, $e8, $1c, $75, $df, $6e,
$47, $f1, $1a, $71, $1d, $29, $c5, $89, $6f, $b7, $62, $0e, $aa, $18, $be, $1b,
$fc, $56, $3e, $4b, $c6, $d2, $79, $20, $9a, $db, $c0, $fe, $78, $cd, $5a, $f4,
$1f, $dd, $a8, $33, $88, $07, $c7, $31, $b1, $12, $10, $59, $27, $80, $ec, $5f,
$60, $51, $7f, $a9, $19, $b5, $4a, $0d, $2d, $e5, $7a, $9f, $93, $c9, $9c, $ef,
$a0, $e0, $3b, $4d, $ae, $2a, $f5, $b0, $c8, $eb, $bb, $3c, $83, $53, $99, $61,
$17, $2b, $04, $7e, $ba, $77, $d6, $26, $e1, $69, $14, $63, $55, $21, $0c, $7d);
const
Td0: array[byte] of longint =
($50a7f451, $5365417e, $c3a4171a, $965e273a, $cb6bab3b, $f1459d1f, $ab58faac, $9303e34b,
$55fa3020, $f66d76ad, $9176cc88, $254c02f5, $fcd7e54f, $d7cb2ac5, $80443526, $8fa362b5,
$495ab1de, $671bba25, $980eea45, $e1c0fe5d, $02752fc3, $12f04c81, $a397468d, $c6f9d36b,
$e75f8f03, $959c9215, $eb7a6dbf, $da595295, $2d83bed4, $d3217458, $2969e049, $44c8c98e,
$6a89c275, $78798ef4, $6b3e5899, $dd71b927, $b64fe1be, $17ad88f0, $66ac20c9, $b43ace7d,
$184adf63, $82311ae5, $60335197, $457f5362, $e07764b1, $84ae6bbb, $1ca081fe, $942b08f9,
$58684870, $19fd458f, $876cde94, $b7f87b52, $23d373ab, $e2024b72, $578f1fe3, $2aab5566,
$0728ebb2, $03c2b52f, $9a7bc586, $a50837d3, $f2872830, $b2a5bf23, $ba6a0302, $5c8216ed,
$2b1ccf8a, $92b479a7, $f0f207f3, $a1e2694e, $cdf4da65, $d5be0506, $1f6234d1, $8afea6c4,
$9d532e34, $a055f3a2, $32e18a05, $75ebf6a4, $39ec830b, $aaef6040, $069f715e, $51106ebd,
$f98a213e, $3d06dd96, $ae053edd, $46bde64d, $b58d5491, $055dc471, $6fd40604, $ff155060,
$24fb9819, $97e9bdd6, $cc434089, $779ed967, $bd42e8b0, $888b8907, $385b19e7, $dbeec879,
$470a7ca1, $e90f427c, $c91e84f8, $00000000, $83868009, $48ed2b32, $ac70111e, $4e725a6c,
$fbff0efd, $5638850f, $1ed5ae3d, $27392d36, $64d90f0a, $21a65c68, $d1545b9b, $3a2e3624,
$b1670a0c, $0fe75793, $d296eeb4, $9e919b1b, $4fc5c080, $a220dc61, $694b775a, $161a121c,
$0aba93e2, $e52aa0c0, $43e0223c, $1d171b12, $0b0d090e, $adc78bf2, $b9a8b62d, $c8a91e14,
$8519f157, $4c0775af, $bbdd99ee, $fd607fa3, $9f2601f7, $bcf5725c, $c53b6644, $347efb5b,
$7629438b, $dcc623cb, $68fcedb6, $63f1e4b8, $cadc31d7, $10856342, $40229713, $2011c684,
$7d244a85, $f83dbbd2, $1132f9ae, $6da129c7, $4b2f9e1d, $f330b2dc, $ec52860d, $d0e3c177,
$6c16b32b, $99b970a9, $fa489411, $2264e947, $c48cfca8, $1a3ff0a0, $d82c7d56, $ef903322,
$c74e4987, $c1d138d9, $fea2ca8c, $360bd498, $cf81f5a6, $28de7aa5, $268eb7da, $a4bfad3f,
$e49d3a2c, $0d927850, $9bcc5f6a, $62467e54, $c2138df6, $e8b8d890, $5ef7392e, $f5afc382,
$be805d9f, $7c93d069, $a92dd56f, $b31225cf, $3b99acc8, $a77d1810, $6e639ce8, $7bbb3bdb,
$097826cd, $f418596e, $01b79aec, $a89a4f83, $656e95e6, $7ee6ffaa, $08cfbc21, $e6e815ef,
$d99be7ba, $ce366f4a, $d4099fea, $d67cb029, $afb2a431, $31233f2a, $3094a5c6, $c066a235,
$37bc4e74, $a6ca82fc, $b0d090e0, $15d8a733, $4a9804f1, $f7daec41, $0e50cd7f, $2ff69117,
$8dd64d76, $4db0ef43, $544daacc, $df0496e4, $e3b5d19e, $1b886a4c, $b81f2cc1, $7f516546,
$04ea5e9d, $5d358c01, $737487fa, $2e410bfb, $5a1d67b3, $52d2db92, $335610e9, $1347d66d,
$8c61d79a, $7a0ca137, $8e14f859, $893c13eb, $ee27a9ce, $35c961b7, $ede51ce1, $3cb1477a,
$59dfd29c, $3f73f255, $79ce1418, $bf37c773, $eacdf753, $5baafd5f, $146f3ddf, $86db4478,
$81f3afca, $3ec468b9, $2c342438, $5f40a3c2, $72c31d16, $0c25e2bc, $8b493c28, $41950dff,
$7101a839, $deb30c08, $9ce4b4d8, $90c15664, $6184cb7b, $70b632d5, $745c6c48, $4257b8d0);
Td1: array[byte] of longint =
($a7f45150, $65417e53, $a4171ac3, $5e273a96, $6bab3bcb, $459d1ff1, $58faacab, $03e34b93,
$fa302055, $6d76adf6, $76cc8891, $4c02f525, $d7e54ffc, $cb2ac5d7, $44352680, $a362b58f,
$5ab1de49, $1bba2567, $0eea4598, $c0fe5de1, $752fc302, $f04c8112, $97468da3, $f9d36bc6,
$5f8f03e7, $9c921595, $7a6dbfeb, $595295da, $83bed42d, $217458d3, $69e04929, $c8c98e44,
$89c2756a, $798ef478, $3e58996b, $71b927dd, $4fe1beb6, $ad88f017, $ac20c966, $3ace7db4,
$4adf6318, $311ae582, $33519760, $7f536245, $7764b1e0, $ae6bbb84, $a081fe1c, $2b08f994,
$68487058, $fd458f19, $6cde9487, $f87b52b7, $d373ab23, $024b72e2, $8f1fe357, $ab55662a,
$28ebb207, $c2b52f03, $7bc5869a, $0837d3a5, $872830f2, $a5bf23b2, $6a0302ba, $8216ed5c,
$1ccf8a2b, $b479a792, $f207f3f0, $e2694ea1, $f4da65cd, $be0506d5, $6234d11f, $fea6c48a,
$532e349d, $55f3a2a0, $e18a0532, $ebf6a475, $ec830b39, $ef6040aa, $9f715e06, $106ebd51,
$8a213ef9, $06dd963d, $053eddae, $bde64d46, $8d5491b5, $5dc47105, $d406046f, $155060ff,
$fb981924, $e9bdd697, $434089cc, $9ed96777, $42e8b0bd, $8b890788, $5b19e738, $eec879db,
$0a7ca147, $0f427ce9, $1e84f8c9, $00000000, $86800983, $ed2b3248, $70111eac, $725a6c4e,
$ff0efdfb, $38850f56, $d5ae3d1e, $392d3627, $d90f0a64, $a65c6821, $545b9bd1, $2e36243a,
$670a0cb1, $e757930f, $96eeb4d2, $919b1b9e, $c5c0804f, $20dc61a2, $4b775a69, $1a121c16,
$ba93e20a, $2aa0c0e5, $e0223c43, $171b121d, $0d090e0b, $c78bf2ad, $a8b62db9, $a91e14c8,
$19f15785, $0775af4c, $dd99eebb, $607fa3fd, $2601f79f, $f5725cbc, $3b6644c5, $7efb5b34,
$29438b76, $c623cbdc, $fcedb668, $f1e4b863, $dc31d7ca, $85634210, $22971340, $11c68420,
$244a857d, $3dbbd2f8, $32f9ae11, $a129c76d, $2f9e1d4b, $30b2dcf3, $52860dec, $e3c177d0,
$16b32b6c, $b970a999, $489411fa, $64e94722, $8cfca8c4, $3ff0a01a, $2c7d56d8, $903322ef,
$4e4987c7, $d138d9c1, $a2ca8cfe, $0bd49836, $81f5a6cf, $de7aa528, $8eb7da26, $bfad3fa4,
$9d3a2ce4, $9278500d, $cc5f6a9b, $467e5462, $138df6c2, $b8d890e8, $f7392e5e, $afc382f5,
$805d9fbe, $93d0697c, $2dd56fa9, $1225cfb3, $99acc83b, $7d1810a7, $639ce86e, $bb3bdb7b,
$7826cd09, $18596ef4, $b79aec01, $9a4f83a8, $6e95e665, $e6ffaa7e, $cfbc2108, $e815efe6,
$9be7bad9, $366f4ace, $099fead4, $7cb029d6, $b2a431af, $233f2a31, $94a5c630, $66a235c0,
$bc4e7437, $ca82fca6, $d090e0b0, $d8a73315, $9804f14a, $daec41f7, $50cd7f0e, $f691172f,
$d64d768d, $b0ef434d, $4daacc54, $0496e4df, $b5d19ee3, $886a4c1b, $1f2cc1b8, $5165467f,
$ea5e9d04, $358c015d, $7487fa73, $410bfb2e, $1d67b35a, $d2db9252, $5610e933, $47d66d13,
$61d79a8c, $0ca1377a, $14f8598e, $3c13eb89, $27a9ceee, $c961b735, $e51ce1ed, $b1477a3c,
$dfd29c59, $73f2553f, $ce141879, $37c773bf, $cdf753ea, $aafd5f5b, $6f3ddf14, $db447886,
$f3afca81, $c468b93e, $3424382c, $40a3c25f, $c31d1672, $25e2bc0c, $493c288b, $950dff41,
$01a83971, $b30c08de, $e4b4d89c, $c1566490, $84cb7b61, $b632d570, $5c6c4874, $57b8d042);
Td2: array[byte] of longint =
($f45150a7, $417e5365, $171ac3a4, $273a965e, $ab3bcb6b, $9d1ff145, $faacab58, $e34b9303,
$302055fa, $76adf66d, $cc889176, $02f5254c, $e54ffcd7, $2ac5d7cb, $35268044, $62b58fa3,
$b1de495a, $ba25671b, $ea45980e, $fe5de1c0, $2fc30275, $4c8112f0, $468da397, $d36bc6f9,
$8f03e75f, $9215959c, $6dbfeb7a, $5295da59, $bed42d83, $7458d321, $e0492969, $c98e44c8,
$c2756a89, $8ef47879, $58996b3e, $b927dd71, $e1beb64f, $88f017ad, $20c966ac, $ce7db43a,
$df63184a, $1ae58231, $51976033, $5362457f, $64b1e077, $6bbb84ae, $81fe1ca0, $08f9942b,
$48705868, $458f19fd, $de94876c, $7b52b7f8, $73ab23d3, $4b72e202, $1fe3578f, $55662aab,
$ebb20728, $b52f03c2, $c5869a7b, $37d3a508, $2830f287, $bf23b2a5, $0302ba6a, $16ed5c82,
$cf8a2b1c, $79a792b4, $07f3f0f2, $694ea1e2, $da65cdf4, $0506d5be, $34d11f62, $a6c48afe,
$2e349d53, $f3a2a055, $8a0532e1, $f6a475eb, $830b39ec, $6040aaef, $715e069f, $6ebd5110,
$213ef98a, $dd963d06, $3eddae05, $e64d46bd, $5491b58d, $c471055d, $06046fd4, $5060ff15,
$981924fb, $bdd697e9, $4089cc43, $d967779e, $e8b0bd42, $8907888b, $19e7385b, $c879dbee,
$7ca1470a, $427ce90f, $84f8c91e, $00000000, $80098386, $2b3248ed, $111eac70, $5a6c4e72,
$0efdfbff, $850f5638, $ae3d1ed5, $2d362739, $0f0a64d9, $5c6821a6, $5b9bd154, $36243a2e,
$0a0cb167, $57930fe7, $eeb4d296, $9b1b9e91, $c0804fc5, $dc61a220, $775a694b, $121c161a,
$93e20aba, $a0c0e52a, $223c43e0, $1b121d17, $090e0b0d, $8bf2adc7, $b62db9a8, $1e14c8a9,
$f1578519, $75af4c07, $99eebbdd, $7fa3fd60, $01f79f26, $725cbcf5, $6644c53b, $fb5b347e,
$438b7629, $23cbdcc6, $edb668fc, $e4b863f1, $31d7cadc, $63421085, $97134022, $c6842011,
$4a857d24, $bbd2f83d, $f9ae1132, $29c76da1, $9e1d4b2f, $b2dcf330, $860dec52, $c177d0e3,
$b32b6c16, $70a999b9, $9411fa48, $e9472264, $fca8c48c, $f0a01a3f, $7d56d82c, $3322ef90,
$4987c74e, $38d9c1d1, $ca8cfea2, $d498360b, $f5a6cf81, $7aa528de, $b7da268e, $ad3fa4bf,
$3a2ce49d, $78500d92, $5f6a9bcc, $7e546246, $8df6c213, $d890e8b8, $392e5ef7, $c382f5af,
$5d9fbe80, $d0697c93, $d56fa92d, $25cfb312, $acc83b99, $1810a77d, $9ce86e63, $3bdb7bbb,
$26cd0978, $596ef418, $9aec01b7, $4f83a89a, $95e6656e, $ffaa7ee6, $bc2108cf, $15efe6e8,
$e7bad99b, $6f4ace36, $9fead409, $b029d67c, $a431afb2, $3f2a3123, $a5c63094, $a235c066,
$4e7437bc, $82fca6ca, $90e0b0d0, $a73315d8, $04f14a98, $ec41f7da, $cd7f0e50, $91172ff6,
$4d768dd6, $ef434db0, $aacc544d, $96e4df04, $d19ee3b5, $6a4c1b88, $2cc1b81f, $65467f51,
$5e9d04ea, $8c015d35, $87fa7374, $0bfb2e41, $67b35a1d, $db9252d2, $10e93356, $d66d1347,
$d79a8c61, $a1377a0c, $f8598e14, $13eb893c, $a9ceee27, $61b735c9, $1ce1ede5, $477a3cb1,
$d29c59df, $f2553f73, $141879ce, $c773bf37, $f753eacd, $fd5f5baa, $3ddf146f, $447886db,
$afca81f3, $68b93ec4, $24382c34, $a3c25f40, $1d1672c3, $e2bc0c25, $3c288b49, $0dff4195,
$a8397101, $0c08deb3, $b4d89ce4, $566490c1, $cb7b6184, $32d570b6, $6c48745c, $b8d04257);
Td3: array[byte] of longint =
($5150a7f4, $7e536541, $1ac3a417, $3a965e27, $3bcb6bab, $1ff1459d, $acab58fa, $4b9303e3,
$2055fa30, $adf66d76, $889176cc, $f5254c02, $4ffcd7e5, $c5d7cb2a, $26804435, $b58fa362,
$de495ab1, $25671bba, $45980eea, $5de1c0fe, $c302752f, $8112f04c, $8da39746, $6bc6f9d3,
$03e75f8f, $15959c92, $bfeb7a6d, $95da5952, $d42d83be, $58d32174, $492969e0, $8e44c8c9,
$756a89c2, $f478798e, $996b3e58, $27dd71b9, $beb64fe1, $f017ad88, $c966ac20, $7db43ace,
$63184adf, $e582311a, $97603351, $62457f53, $b1e07764, $bb84ae6b, $fe1ca081, $f9942b08,
$70586848, $8f19fd45, $94876cde, $52b7f87b, $ab23d373, $72e2024b, $e3578f1f, $662aab55,
$b20728eb, $2f03c2b5, $869a7bc5, $d3a50837, $30f28728, $23b2a5bf, $02ba6a03, $ed5c8216,
$8a2b1ccf, $a792b479, $f3f0f207, $4ea1e269, $65cdf4da, $06d5be05, $d11f6234, $c48afea6,
$349d532e, $a2a055f3, $0532e18a, $a475ebf6, $0b39ec83, $40aaef60, $5e069f71, $bd51106e,
$3ef98a21, $963d06dd, $ddae053e, $4d46bde6, $91b58d54, $71055dc4, $046fd406, $60ff1550,
$1924fb98, $d697e9bd, $89cc4340, $67779ed9, $b0bd42e8, $07888b89, $e7385b19, $79dbeec8,
$a1470a7c, $7ce90f42, $f8c91e84, $00000000, $09838680, $3248ed2b, $1eac7011, $6c4e725a,
$fdfbff0e, $0f563885, $3d1ed5ae, $3627392d, $0a64d90f, $6821a65c, $9bd1545b, $243a2e36,
$0cb1670a, $930fe757, $b4d296ee, $1b9e919b, $804fc5c0, $61a220dc, $5a694b77, $1c161a12,
$e20aba93, $c0e52aa0, $3c43e022, $121d171b, $0e0b0d09, $f2adc78b, $2db9a8b6, $14c8a91e,
$578519f1, $af4c0775, $eebbdd99, $a3fd607f, $f79f2601, $5cbcf572, $44c53b66, $5b347efb,
$8b762943, $cbdcc623, $b668fced, $b863f1e4, $d7cadc31, $42108563, $13402297, $842011c6,
$857d244a, $d2f83dbb, $ae1132f9, $c76da129, $1d4b2f9e, $dcf330b2, $0dec5286, $77d0e3c1,
$2b6c16b3, $a999b970, $11fa4894, $472264e9, $a8c48cfc, $a01a3ff0, $56d82c7d, $22ef9033,
$87c74e49, $d9c1d138, $8cfea2ca, $98360bd4, $a6cf81f5, $a528de7a, $da268eb7, $3fa4bfad,
$2ce49d3a, $500d9278, $6a9bcc5f, $5462467e, $f6c2138d, $90e8b8d8, $2e5ef739, $82f5afc3,
$9fbe805d, $697c93d0, $6fa92dd5, $cfb31225, $c83b99ac, $10a77d18, $e86e639c, $db7bbb3b,
$cd097826, $6ef41859, $ec01b79a, $83a89a4f, $e6656e95, $aa7ee6ff, $2108cfbc, $efe6e815,
$bad99be7, $4ace366f, $ead4099f, $29d67cb0, $31afb2a4, $2a31233f, $c63094a5, $35c066a2,
$7437bc4e, $fca6ca82, $e0b0d090, $3315d8a7, $f14a9804, $41f7daec, $7f0e50cd, $172ff691,
$768dd64d, $434db0ef, $cc544daa, $e4df0496, $9ee3b5d1, $4c1b886a, $c1b81f2c, $467f5165,
$9d04ea5e, $015d358c, $fa737487, $fb2e410b, $b35a1d67, $9252d2db, $e9335610, $6d1347d6,
$9a8c61d7, $377a0ca1, $598e14f8, $eb893c13, $ceee27a9, $b735c961, $e1ede51c, $7a3cb147,
$9c59dfd2, $553f73f2, $1879ce14, $73bf37c7, $53eacdf7, $5f5baafd, $df146f3d, $7886db44,
$ca81f3af, $b93ec468, $382c3424, $c25f40a3, $1672c31d, $bc0c25e2, $288b493c, $ff41950d,
$397101a8, $08deb30c, $d89ce4b4, $6490c156, $7b6184cb, $d570b632, $48745c6c, $d04257b8);
{$ifdef AES_LONGBOX}
Td4: array[byte] of longint =
($52525252, $09090909, $6a6a6a6a, $d5d5d5d5, $30303030, $36363636, $a5a5a5a5, $38383838,
$bfbfbfbf, $40404040, $a3a3a3a3, $9e9e9e9e, $81818181, $f3f3f3f3, $d7d7d7d7, $fbfbfbfb,
$7c7c7c7c, $e3e3e3e3, $39393939, $82828282, $9b9b9b9b, $2f2f2f2f, $ffffffff, $87878787,
$34343434, $8e8e8e8e, $43434343, $44444444, $c4c4c4c4, $dededede, $e9e9e9e9, $cbcbcbcb,
$54545454, $7b7b7b7b, $94949494, $32323232, $a6a6a6a6, $c2c2c2c2, $23232323, $3d3d3d3d,
$eeeeeeee, $4c4c4c4c, $95959595, $0b0b0b0b, $42424242, $fafafafa, $c3c3c3c3, $4e4e4e4e,
$08080808, $2e2e2e2e, $a1a1a1a1, $66666666, $28282828, $d9d9d9d9, $24242424, $b2b2b2b2,
$76767676, $5b5b5b5b, $a2a2a2a2, $49494949, $6d6d6d6d, $8b8b8b8b, $d1d1d1d1, $25252525,
$72727272, $f8f8f8f8, $f6f6f6f6, $64646464, $86868686, $68686868, $98989898, $16161616,
$d4d4d4d4, $a4a4a4a4, $5c5c5c5c, $cccccccc, $5d5d5d5d, $65656565, $b6b6b6b6, $92929292,
$6c6c6c6c, $70707070, $48484848, $50505050, $fdfdfdfd, $edededed, $b9b9b9b9, $dadadada,
$5e5e5e5e, $15151515, $46464646, $57575757, $a7a7a7a7, $8d8d8d8d, $9d9d9d9d, $84848484,
$90909090, $d8d8d8d8, $abababab, $00000000, $8c8c8c8c, $bcbcbcbc, $d3d3d3d3, $0a0a0a0a,
$f7f7f7f7, $e4e4e4e4, $58585858, $05050505, $b8b8b8b8, $b3b3b3b3, $45454545, $06060606,
$d0d0d0d0, $2c2c2c2c, $1e1e1e1e, $8f8f8f8f, $cacacaca, $3f3f3f3f, $0f0f0f0f, $02020202,
$c1c1c1c1, $afafafaf, $bdbdbdbd, $03030303, $01010101, $13131313, $8a8a8a8a, $6b6b6b6b,
$3a3a3a3a, $91919191, $11111111, $41414141, $4f4f4f4f, $67676767, $dcdcdcdc, $eaeaeaea,
$97979797, $f2f2f2f2, $cfcfcfcf, $cececece, $f0f0f0f0, $b4b4b4b4, $e6e6e6e6, $73737373,
$96969696, $acacacac, $74747474, $22222222, $e7e7e7e7, $adadadad, $35353535, $85858585,
$e2e2e2e2, $f9f9f9f9, $37373737, $e8e8e8e8, $1c1c1c1c, $75757575, $dfdfdfdf, $6e6e6e6e,
$47474747, $f1f1f1f1, $1a1a1a1a, $71717171, $1d1d1d1d, $29292929, $c5c5c5c5, $89898989,
$6f6f6f6f, $b7b7b7b7, $62626262, $0e0e0e0e, $aaaaaaaa, $18181818, $bebebebe, $1b1b1b1b,
$fcfcfcfc, $56565656, $3e3e3e3e, $4b4b4b4b, $c6c6c6c6, $d2d2d2d2, $79797979, $20202020,
$9a9a9a9a, $dbdbdbdb, $c0c0c0c0, $fefefefe, $78787878, $cdcdcdcd, $5a5a5a5a, $f4f4f4f4,
$1f1f1f1f, $dddddddd, $a8a8a8a8, $33333333, $88888888, $07070707, $c7c7c7c7, $31313131,
$b1b1b1b1, $12121212, $10101010, $59595959, $27272727, $80808080, $ecececec, $5f5f5f5f,
$60606060, $51515151, $7f7f7f7f, $a9a9a9a9, $19191919, $b5b5b5b5, $4a4a4a4a, $0d0d0d0d,
$2d2d2d2d, $e5e5e5e5, $7a7a7a7a, $9f9f9f9f, $93939393, $c9c9c9c9, $9c9c9c9c, $efefefef,
$a0a0a0a0, $e0e0e0e0, $3b3b3b3b, $4d4d4d4d, $aeaeaeae, $2a2a2a2a, $f5f5f5f5, $b0b0b0b0,
$c8c8c8c8, $ebebebeb, $bbbbbbbb, $3c3c3c3c, $83838383, $53535353, $99999999, $61616161,
$17171717, $2b2b2b2b, $04040404, $7e7e7e7e, $babababa, $77777777, $d6d6d6d6, $26262626,
$e1e1e1e1, $69696969, $14141414, $63636363, $55555555, $21212121, $0c0c0c0c, $7d7d7d7d);
{$endif}
{$ifdef StrictLong}
{$warnings on}
{$ifdef RangeChecks_on}
{$R+}
{$endif}
{$endif}
{$ifdef AES_LONGBOX}
const
X000000ff = longint($000000ff); {Avoid D4+ warnings}
X0000ff00 = longint($0000ff00);
X00ff0000 = longint($00ff0000);
Xff000000 = longint($ff000000);
{$endif}

View File

@ -0,0 +1,92 @@
(*************************************************************************
Include file for AES_DECR.PAS - AES_Decrypt for Pascal16/Full tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version from AES_DECR.PAS
0.11 15.11.08 we Use Ptr2Inc from BTypes
**************************************************************************)
(**** (C) Copyright 2002-2008 Wolfgang Ehrhardt -- see copying_we.txt ****)
{Normally used for TP5/5.5 (and during development BP7)}
{---------------------------------------------------------------------------}
procedure AES_Decrypt(var ctx: TAESContext; {$ifdef CONST} const {$else} var {$endif} BI: TAESBlock; var BO: TAESBlock);
{-decrypt one block (in ECB mode)}
label done;
var
r: integer;
pK: PWA4; {pointer to loop rount key }
s,t: TAESBlock;
begin
{Setup key pointer}
pK := PWA4(@ctx.RK[ctx.Rounds]);
{Initialize with input block}
TWA4(s)[0] := TWA4(BI)[0] xor pK^[0];
TWA4(s)[1] := TWA4(BI)[1] xor pK^[1];
TWA4(s)[2] := TWA4(BI)[2] xor pK^[2];
TWA4(s)[3] := TWA4(BI)[3] xor pK^[3];
dec(Ptr2Inc(pK), 4*sizeof(longint));
r := ctx.Rounds-1;
while true do begin
TWA4(t)[3] := Td0[s[3*4+0]] xor Td1[s[2*4+1]] xor Td2[s[1*4+2]] xor Td3[s[0*4+3]] xor pK^[3];
TWA4(t)[2] := Td0[s[2*4+0]] xor Td1[s[1*4+1]] xor Td2[s[0*4+2]] xor Td3[s[3*4+3]] xor pK^[2];
TWA4(t)[1] := Td0[s[1*4+0]] xor Td1[s[0*4+1]] xor Td2[s[3*4+2]] xor Td3[s[2*4+3]] xor pK^[1];
TWA4(t)[0] := Td0[s[0*4+0]] xor Td1[s[3*4+1]] xor Td2[s[2*4+2]] xor Td3[s[1*4+3]] xor pK^[0];
dec(Ptr2Inc(pK), 4*sizeof(longint));
dec(r);
if r<1 then goto done;
TWA4(s)[3] := Td0[t[3*4+0]] xor Td1[t[2*4+1]] xor Td2[t[1*4+2]] xor Td3[t[0*4+3]] xor pK^[3];
TWA4(s)[2] := Td0[t[2*4+0]] xor Td1[t[1*4+1]] xor Td2[t[0*4+2]] xor Td3[t[3*4+3]] xor pK^[2];
TWA4(s)[1] := Td0[t[1*4+0]] xor Td1[t[0*4+1]] xor Td2[t[3*4+2]] xor Td3[t[2*4+3]] xor pK^[1];
TWA4(s)[0] := Td0[t[0*4+0]] xor Td1[t[3*4+1]] xor Td2[t[2*4+2]] xor Td3[t[1*4+3]] xor pK^[0];
dec(Ptr2Inc(pK), 4*sizeof(longint));
dec(r);
end;
done:
s[00] := InvSBox[t[0*4+0]];
s[01] := InvSBox[t[3*4+1]];
s[02] := InvSBox[t[2*4+2]];
s[03] := InvSBox[t[1*4+3]];
s[04] := InvSBox[t[1*4+0]];
s[05] := InvSBox[t[0*4+1]];
s[06] := InvSBox[t[3*4+2]];
s[07] := InvSBox[t[2*4+3]];
s[08] := InvSBox[t[2*4+0]];
s[09] := InvSBox[t[1*4+1]];
s[10] := InvSBox[t[0*4+2]];
s[11] := InvSBox[t[3*4+3]];
s[12] := InvSBox[t[3*4+0]];
s[13] := InvSBox[t[2*4+1]];
s[14] := InvSBox[t[1*4+2]];
s[15] := InvSBox[t[0*4+3]];
TWA4(BO)[0] := TWA4(s)[0] xor pK^[0];
TWA4(BO)[1] := TWA4(s)[1] xor pK^[1];
TWA4(BO)[2] := TWA4(s)[2] xor pK^[2];
TWA4(BO)[3] := TWA4(s)[3] xor pK^[3];
end;
{---------------------------------------------------------------------------}
procedure MakeDecrKey(var ctx: TAESContext);
{-Calculate decryption key from encryption key}
var
i: integer;
x: longint;
t: TBA4 absolute x;
begin
with ctx do begin
for i:=4 to 4*Rounds-1 do begin
{Inverse MixColumns transformation: use Sbox and}
{implicit endian conversion compared with [2] }
x := TAWK(RK)[i];
TAWK(RK)[i] := Td3[SBox[t[3]]] xor Td2[SBox[t[2]]] xor Td1[SBox[t[1]]] xor Td0[SBox[t[0]]];
end;
end;
end;

View File

@ -0,0 +1,106 @@
(*************************************************************************
Include file for AES_DECR.PAS - AES_Decrypt for BIT32/Full tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version from AES_DECR.PAS
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{ 32 Bit code: Alternative versions can be found in options.zip
dec_full.inc - fully unrolled version for highest speed
dec_ptr.inc - pointer version (may be faster on some systems)
}
{---------------------------------------------------------------------------}
procedure AES_Decrypt(var ctx: TAESContext; const BI: TAESBlock; var BO: TAESBlock);
{-decrypt one block (in ECB mode)}
var
r: integer; {round loop countdown counter}
pK: PWA4; {pointer to loop rount key }
s0,s1,s2,s3: longint; {TAESBlock s as separate variables}
t: TWA4;
begin
{Setup key pointer}
pK := PWA4(@ctx.RK[ctx.Rounds]);
{Initialize with input block}
s0 := TWA4(BI)[0] xor pK^[0];
s1 := TWA4(BI)[1] xor pK^[1];
s2 := TWA4(BI)[2] xor pK^[2];
s3 := TWA4(BI)[3] xor pK^[3];
dec(pK);
{perform encryption rounds}
for r:=1 to ctx.Rounds-1 do begin
t[3] := Td0[s3 and $ff] xor Td1[s2 shr 8 and $ff] xor Td2[s1 shr 16 and $ff] xor Td3[s0 shr 24] xor pK^[3];
t[2] := Td0[s2 and $ff] xor Td1[s1 shr 8 and $ff] xor Td2[s0 shr 16 and $ff] xor Td3[s3 shr 24] xor pK^[2];
t[1] := Td0[s1 and $ff] xor Td1[s0 shr 8 and $ff] xor Td2[s3 shr 16 and $ff] xor Td3[s2 shr 24] xor pK^[1];
s0 := Td0[s0 and $ff] xor Td1[s3 shr 8 and $ff] xor Td2[s2 shr 16 and $ff] xor Td3[s1 shr 24] xor pK^[0];
s1 := t[1];
s2 := t[2];
s3 := t[3];
dec(pK);
end;
{$ifdef AES_LONGBOX}
{Use expanded longint InvSBox table Td4 from [2]}
TWA4(BO)[0] := (Td4[s0 and $ff] and X000000ff) xor
(Td4[s3 shr 8 and $ff] and X0000ff00) xor
(Td4[s2 shr 16 and $ff] and X00ff0000) xor
(Td4[s1 shr 24 ] and Xff000000) xor pK^[0];
TWA4(BO)[1] := (Td4[s1 and $ff] and X000000ff) xor
(Td4[s0 shr 8 and $ff] and X0000ff00) xor
(Td4[s3 shr 16 and $ff] and X00ff0000) xor
(Td4[s2 shr 24 ] and Xff000000) xor pK^[1];
TWA4(BO)[2] := (Td4[s2 and $ff ] and X000000ff) xor
(Td4[s1 shr 8 and $ff] and X0000ff00) xor
(Td4[s0 shr 16 and $ff] and X00ff0000) xor
(Td4[s3 shr 24 ] and Xff000000) xor pK^[2];
TWA4(BO)[3] := (Td4[s3 and $ff ] and X000000ff) xor
(Td4[s2 shr 8 and $ff] and X0000ff00) xor
(Td4[s1 shr 16 and $ff] and X00ff0000) xor
(Td4[s0 shr 24 ] and Xff000000) xor pK^[3];
{$else}
{Uses InvSbox and shl, needs type cast longint() for }
{16 bit compilers: here InvSbox is byte, Td4 is longint}
TWA4(BO)[0] := (longint(InvSBox[s0 and $ff]) xor
longint(InvSBox[s3 shr 8 and $ff]) shl 8 xor
longint(InvSBox[s2 shr 16 and $ff]) shl 16 xor
longint(InvSBox[s1 shr 24 ]) shl 24 ) xor pK^[0];
TWA4(BO)[1] := (longint(InvSBox[s1 and $ff]) xor
longint(InvSBox[s0 shr 8 and $ff]) shl 8 xor
longint(InvSBox[s3 shr 16 and $ff]) shl 16 xor
longint(InvSBox[s2 shr 24 ]) shl 24 ) xor pK^[1];
TWA4(BO)[2] := (longint(InvSBox[s2 and $ff ]) xor
longint(InvSBox[s1 shr 8 and $ff]) shl 8 xor
longint(InvSBox[s0 shr 16 and $ff]) shl 16 xor
longint(InvSBox[s3 shr 24 ]) shl 24 ) xor pK^[2];
TWA4(BO)[3] := (longint(InvSBox[s3 and $ff ]) xor
longint(InvSBox[s2 shr 8 and $ff]) shl 8 xor
longint(InvSBox[s1 shr 16 and $ff]) shl 16 xor
longint(InvSBox[s0 shr 24 ]) shl 24 ) xor pK^[3];
{$endif}
end;
{---------------------------------------------------------------------------}
procedure MakeDecrKey(var ctx: TAESContext);
{-Calculate decryption key from encryption key}
var
i: integer;
p: PLong;
x: longint;
begin
p := PLong(@ctx.RK[1]);
for i:=1 to 4*(ctx.Rounds-1) do begin
x := p^;
p^ := Td3[SBox[x shr 24]] xor Td2[SBox[x shr 16 and $ff]] xor Td1[SBox[x shr 8 and $ff]] xor Td0[SBox[x and $ff]];
inc(p);
end;
end;

View File

@ -0,0 +1,350 @@
(*************************************************************************
Include file for AES_ENCR.PAS - AES_Encrypt for BASM16/Compressed table
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version for compressed table
0.11 10.07.06 we Removed bx in TCe[bx+si+?]
0.13 13.07.06 we Uses TCe box byte instead of SBox
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{16 bit BASM used for TP6, BP7, Delphi1}
{---------------------------------------------------------------------------}
procedure AES_Encrypt(var ctx: TAESContext; {$ifdef CONST} const {$else} var {$endif} BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
var
s,t: TAESBlock;
rnd: integer;
pK: pointer;
begin
rnd := ctx.rounds;
pK := @ctx.RK;
asm
db $66; pusha
{AES_XorBlock(BI, ctx.RK[0], s);}
les si,[BI]
db $66; mov ax,es:[si]
db $66; mov bx,es:[si+4]
db $66; mov cx,es:[si+8]
db $66; mov dx,es:[si+12]
les di,[pK]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
db $66; mov word ptr [s],ax
db $66; mov word ptr [s+4],bx
db $66; mov word ptr [s+8],cx
db $66; mov word ptr [s+12],dx
add di,16 {di->ctx.RK[1]}
mov cx,[rnd]
dec cx
{ *Note* in the following round loop }
{ op eax, mem[8*ebx] is calculated as }
{ lea esi, [edx+8*ebx] $66,$67,$8D,$34,$DA }
{ op eax, mem[esi] }
db $66; sub bx,bx {clear ebx}
db $66; sub dx,dx {clear edx}
@@1:
{TWA4(t)[0] := Te0[s[0*4+0]] xor Te1[s[1*4+1]] xor Te2[s[2*4+2]] xor Te3[s[3*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr s[0*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCe[si+3]
mov bl,byte ptr s[1*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+2]
mov bl,byte ptr s[2*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+1]
mov bl,byte ptr s[3*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si]
db $66; xor ax,es:[di]
db $66; mov word ptr t[0],ax
{TWA4(t)[1] := Te0[s[1*4+0]] xor Te1[s[2*4+1]] xor Te2[s[3*4+2]] xor Te3[s[0*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr s[1*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCe[si+3]
mov bl,byte ptr s[2*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+2]
mov bl,byte ptr s[3*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+1]
mov bl,byte ptr s[0*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si]
db $66; xor ax,es:[di+4]
db $66; mov word ptr t[4],ax
{TWA4(t)[2] := Te0[s[2*4+0]] xor Te1[s[3*4+1]] xor Te2[s[0*4+2]] xor Te3[s[1*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr s[2*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCe[si+3]
mov bl,byte ptr s[3*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+2]
mov bl,byte ptr s[0*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+1]
mov bl,byte ptr s[1*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si]
db $66; xor ax,es:[di+8]
db $66; mov word ptr t[8],ax
{TWA4(t)[3] := Te0[s[3*4+0]] xor Te1[s[0*4+1]] xor Te2[s[1*4+2]] xor Te3[s[2*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr s[3*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCe[si+3]
mov bl,byte ptr s[0*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+2]
mov bl,byte ptr s[1*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+1]
mov bl,byte ptr s[2*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si]
db $66; xor ax,es:[di+12]
db $66; mov word ptr t[12],ax
{if r>=ctx.rounds then break;}
dec cx
jbe @@2
{TWA4(s)[0] := Te0[t[0*4+0]] xor Te1[t[1*4+1]] xor Te2[t[2*4+2]] xor Te3[t[3*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr t[0*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCe[si+3]
mov bl,byte ptr t[1*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+2]
mov bl,byte ptr t[2*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+1]
mov bl,byte ptr t[3*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si]
db $66; xor ax,es:[di+16]
db $66; mov word ptr s[0],ax
{TWA4(s)[1] := Te0[t[1*4+0]] xor Te1[t[2*4+1]] xor Te2[t[3*4+2]] xor Te3[t[0*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr t[1*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCe[si+3]
mov bl,byte ptr t[2*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+2]
mov bl,byte ptr t[3*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+1]
mov bl,byte ptr t[0*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si]
db $66; xor ax,es:[di+20]
db $66; mov word ptr s[4],ax
{TWA4(s)[2] := Te0[t[2*4+0]] xor Te1[t[3*4+1]] xor Te2[t[0*4+2]] xor Te3[t[1*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr t[2*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCe[si+3]
mov bl,byte ptr t[3*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+2]
mov bl,byte ptr t[0*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+1]
mov bl,byte ptr t[1*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si]
db $66; xor ax,es:[di+24]
db $66; mov word ptr s[8],ax
{TWA4(s)[3] := Te0[t[3*4+0]] xor Te1[t[0*4+1]] xor Te2[t[1*4+2]] xor Te3[t[2*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr t[3*4+0]
db $66,$67,$8D,$34,$DA;
db $66; mov ax,word ptr TCe[si+3]
mov bl,byte ptr t[0*4+1]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+2]
mov bl,byte ptr t[1*4+2]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si+1]
mov bl,byte ptr t[2*4+3]
db $66,$67,$8D,$34,$DA;
db $66; xor ax,word ptr TCe[si]
db $66; xor ax,es:[di+28]
add di,32
db $66; mov word ptr s[12],ax
dec cx
jmp @@1
@@2: add di,16 {di -> ctx.RK[ctx.rounds]}
{Last round uses SBox}
mov bl, byte ptr t[0*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[0],al
mov bl, byte ptr t[1*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[1],al
mov bl, byte ptr t[2*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[2],al
mov bl, byte ptr t[3*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[3],al
mov bl, byte ptr t[1*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[4],al
mov bl, byte ptr t[2*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[5],al
mov bl, byte ptr t[3*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[6],al
mov bl, byte ptr t[0*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[7],al
mov bl, byte ptr t[2*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[8],al
mov bl, byte ptr t[3*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[9],al
mov bl, byte ptr t[0*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[10],al
mov bl, byte ptr t[1*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[11],al
mov bl, byte ptr t[3*4+0]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[12],al
mov bl, byte ptr t[0*4+1]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[13],al
mov bl, byte ptr t[1*4+2]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[14],al
mov bl, byte ptr t[2*4+3]
sub bh,bh
shl bx,3
mov al, byte ptr TCe[bx+7]
mov byte ptr s[15],al
{AES_XorBlock(s, ctx.RK[rnd], BO)}
db $66; mov ax,word ptr [s]
db $66; mov bx,word ptr [s+4]
db $66; mov cx,word ptr [s+8]
db $66; mov dx,word ptr [s+12]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
les si,[BO]
db $66; mov es:[si],ax
db $66; mov es:[si+4],bx
db $66; mov es:[si+8],cx
db $66; mov es:[si+12],dx
db $66; popa
end;
end;

View File

@ -0,0 +1,196 @@
(*************************************************************************
Include file for AES_ENCR.PAS - Compressed tables/Helper types
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version for compressed tables
0.11 13.07.06 we Removed AES_LONGBOX consts, b3 gets box byte
0.12 19.07.06 we TCeDummy
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
type
TH3 = packed record
L: longint;
b0,b1,b2,box: byte;
end;
TH2 = packed record
b0: byte;
L: longint;
b1,b2,box: byte;
end;
TH1 = packed record
b0,b1: byte;
L: longint;
b2,box: byte;
end;
TH0 = packed record
b0,b1,b2: byte;
L: longint;
box: byte;
end;
TEU = record
case integer of
0: (E0: TH0);
1: (E1: TH1);
2: (E2: TH2);
3: (E3: TH3);
end;
{$ifdef StrictLong}
{$warnings off}
{$R-} {avoid D9+ errors!}
{$endif}
const
{$ifdef AES_Encr_DummyAlign}
TCeDummy : longint = 0; {Use to align TCe to 8 byte boundary}
{$endif}
TCe: packed array[0..2047] of byte = (
$63,$63,$a5,$c6,$63,$63,$a5,$63,$7c,$7c,$84,$f8,$7c,$7c,$84,$7c,
$77,$77,$99,$ee,$77,$77,$99,$77,$7b,$7b,$8d,$f6,$7b,$7b,$8d,$7b,
$f2,$f2,$0d,$ff,$f2,$f2,$0d,$f2,$6b,$6b,$bd,$d6,$6b,$6b,$bd,$6b,
$6f,$6f,$b1,$de,$6f,$6f,$b1,$6f,$c5,$c5,$54,$91,$c5,$c5,$54,$c5,
$30,$30,$50,$60,$30,$30,$50,$30,$01,$01,$03,$02,$01,$01,$03,$01,
$67,$67,$a9,$ce,$67,$67,$a9,$67,$2b,$2b,$7d,$56,$2b,$2b,$7d,$2b,
$fe,$fe,$19,$e7,$fe,$fe,$19,$fe,$d7,$d7,$62,$b5,$d7,$d7,$62,$d7,
$ab,$ab,$e6,$4d,$ab,$ab,$e6,$ab,$76,$76,$9a,$ec,$76,$76,$9a,$76,
$ca,$ca,$45,$8f,$ca,$ca,$45,$ca,$82,$82,$9d,$1f,$82,$82,$9d,$82,
$c9,$c9,$40,$89,$c9,$c9,$40,$c9,$7d,$7d,$87,$fa,$7d,$7d,$87,$7d,
$fa,$fa,$15,$ef,$fa,$fa,$15,$fa,$59,$59,$eb,$b2,$59,$59,$eb,$59,
$47,$47,$c9,$8e,$47,$47,$c9,$47,$f0,$f0,$0b,$fb,$f0,$f0,$0b,$f0,
$ad,$ad,$ec,$41,$ad,$ad,$ec,$ad,$d4,$d4,$67,$b3,$d4,$d4,$67,$d4,
$a2,$a2,$fd,$5f,$a2,$a2,$fd,$a2,$af,$af,$ea,$45,$af,$af,$ea,$af,
$9c,$9c,$bf,$23,$9c,$9c,$bf,$9c,$a4,$a4,$f7,$53,$a4,$a4,$f7,$a4,
$72,$72,$96,$e4,$72,$72,$96,$72,$c0,$c0,$5b,$9b,$c0,$c0,$5b,$c0,
$b7,$b7,$c2,$75,$b7,$b7,$c2,$b7,$fd,$fd,$1c,$e1,$fd,$fd,$1c,$fd,
$93,$93,$ae,$3d,$93,$93,$ae,$93,$26,$26,$6a,$4c,$26,$26,$6a,$26,
$36,$36,$5a,$6c,$36,$36,$5a,$36,$3f,$3f,$41,$7e,$3f,$3f,$41,$3f,
$f7,$f7,$02,$f5,$f7,$f7,$02,$f7,$cc,$cc,$4f,$83,$cc,$cc,$4f,$cc,
$34,$34,$5c,$68,$34,$34,$5c,$34,$a5,$a5,$f4,$51,$a5,$a5,$f4,$a5,
$e5,$e5,$34,$d1,$e5,$e5,$34,$e5,$f1,$f1,$08,$f9,$f1,$f1,$08,$f1,
$71,$71,$93,$e2,$71,$71,$93,$71,$d8,$d8,$73,$ab,$d8,$d8,$73,$d8,
$31,$31,$53,$62,$31,$31,$53,$31,$15,$15,$3f,$2a,$15,$15,$3f,$15,
$04,$04,$0c,$08,$04,$04,$0c,$04,$c7,$c7,$52,$95,$c7,$c7,$52,$c7,
$23,$23,$65,$46,$23,$23,$65,$23,$c3,$c3,$5e,$9d,$c3,$c3,$5e,$c3,
$18,$18,$28,$30,$18,$18,$28,$18,$96,$96,$a1,$37,$96,$96,$a1,$96,
$05,$05,$0f,$0a,$05,$05,$0f,$05,$9a,$9a,$b5,$2f,$9a,$9a,$b5,$9a,
$07,$07,$09,$0e,$07,$07,$09,$07,$12,$12,$36,$24,$12,$12,$36,$12,
$80,$80,$9b,$1b,$80,$80,$9b,$80,$e2,$e2,$3d,$df,$e2,$e2,$3d,$e2,
$eb,$eb,$26,$cd,$eb,$eb,$26,$eb,$27,$27,$69,$4e,$27,$27,$69,$27,
$b2,$b2,$cd,$7f,$b2,$b2,$cd,$b2,$75,$75,$9f,$ea,$75,$75,$9f,$75,
$09,$09,$1b,$12,$09,$09,$1b,$09,$83,$83,$9e,$1d,$83,$83,$9e,$83,
$2c,$2c,$74,$58,$2c,$2c,$74,$2c,$1a,$1a,$2e,$34,$1a,$1a,$2e,$1a,
$1b,$1b,$2d,$36,$1b,$1b,$2d,$1b,$6e,$6e,$b2,$dc,$6e,$6e,$b2,$6e,
$5a,$5a,$ee,$b4,$5a,$5a,$ee,$5a,$a0,$a0,$fb,$5b,$a0,$a0,$fb,$a0,
$52,$52,$f6,$a4,$52,$52,$f6,$52,$3b,$3b,$4d,$76,$3b,$3b,$4d,$3b,
$d6,$d6,$61,$b7,$d6,$d6,$61,$d6,$b3,$b3,$ce,$7d,$b3,$b3,$ce,$b3,
$29,$29,$7b,$52,$29,$29,$7b,$29,$e3,$e3,$3e,$dd,$e3,$e3,$3e,$e3,
$2f,$2f,$71,$5e,$2f,$2f,$71,$2f,$84,$84,$97,$13,$84,$84,$97,$84,
$53,$53,$f5,$a6,$53,$53,$f5,$53,$d1,$d1,$68,$b9,$d1,$d1,$68,$d1,
$00,$00,$00,$00,$00,$00,$00,$00,$ed,$ed,$2c,$c1,$ed,$ed,$2c,$ed,
$20,$20,$60,$40,$20,$20,$60,$20,$fc,$fc,$1f,$e3,$fc,$fc,$1f,$fc,
$b1,$b1,$c8,$79,$b1,$b1,$c8,$b1,$5b,$5b,$ed,$b6,$5b,$5b,$ed,$5b,
$6a,$6a,$be,$d4,$6a,$6a,$be,$6a,$cb,$cb,$46,$8d,$cb,$cb,$46,$cb,
$be,$be,$d9,$67,$be,$be,$d9,$be,$39,$39,$4b,$72,$39,$39,$4b,$39,
$4a,$4a,$de,$94,$4a,$4a,$de,$4a,$4c,$4c,$d4,$98,$4c,$4c,$d4,$4c,
$58,$58,$e8,$b0,$58,$58,$e8,$58,$cf,$cf,$4a,$85,$cf,$cf,$4a,$cf,
$d0,$d0,$6b,$bb,$d0,$d0,$6b,$d0,$ef,$ef,$2a,$c5,$ef,$ef,$2a,$ef,
$aa,$aa,$e5,$4f,$aa,$aa,$e5,$aa,$fb,$fb,$16,$ed,$fb,$fb,$16,$fb,
$43,$43,$c5,$86,$43,$43,$c5,$43,$4d,$4d,$d7,$9a,$4d,$4d,$d7,$4d,
$33,$33,$55,$66,$33,$33,$55,$33,$85,$85,$94,$11,$85,$85,$94,$85,
$45,$45,$cf,$8a,$45,$45,$cf,$45,$f9,$f9,$10,$e9,$f9,$f9,$10,$f9,
$02,$02,$06,$04,$02,$02,$06,$02,$7f,$7f,$81,$fe,$7f,$7f,$81,$7f,
$50,$50,$f0,$a0,$50,$50,$f0,$50,$3c,$3c,$44,$78,$3c,$3c,$44,$3c,
$9f,$9f,$ba,$25,$9f,$9f,$ba,$9f,$a8,$a8,$e3,$4b,$a8,$a8,$e3,$a8,
$51,$51,$f3,$a2,$51,$51,$f3,$51,$a3,$a3,$fe,$5d,$a3,$a3,$fe,$a3,
$40,$40,$c0,$80,$40,$40,$c0,$40,$8f,$8f,$8a,$05,$8f,$8f,$8a,$8f,
$92,$92,$ad,$3f,$92,$92,$ad,$92,$9d,$9d,$bc,$21,$9d,$9d,$bc,$9d,
$38,$38,$48,$70,$38,$38,$48,$38,$f5,$f5,$04,$f1,$f5,$f5,$04,$f5,
$bc,$bc,$df,$63,$bc,$bc,$df,$bc,$b6,$b6,$c1,$77,$b6,$b6,$c1,$b6,
$da,$da,$75,$af,$da,$da,$75,$da,$21,$21,$63,$42,$21,$21,$63,$21,
$10,$10,$30,$20,$10,$10,$30,$10,$ff,$ff,$1a,$e5,$ff,$ff,$1a,$ff,
$f3,$f3,$0e,$fd,$f3,$f3,$0e,$f3,$d2,$d2,$6d,$bf,$d2,$d2,$6d,$d2,
$cd,$cd,$4c,$81,$cd,$cd,$4c,$cd,$0c,$0c,$14,$18,$0c,$0c,$14,$0c,
$13,$13,$35,$26,$13,$13,$35,$13,$ec,$ec,$2f,$c3,$ec,$ec,$2f,$ec,
$5f,$5f,$e1,$be,$5f,$5f,$e1,$5f,$97,$97,$a2,$35,$97,$97,$a2,$97,
$44,$44,$cc,$88,$44,$44,$cc,$44,$17,$17,$39,$2e,$17,$17,$39,$17,
$c4,$c4,$57,$93,$c4,$c4,$57,$c4,$a7,$a7,$f2,$55,$a7,$a7,$f2,$a7,
$7e,$7e,$82,$fc,$7e,$7e,$82,$7e,$3d,$3d,$47,$7a,$3d,$3d,$47,$3d,
$64,$64,$ac,$c8,$64,$64,$ac,$64,$5d,$5d,$e7,$ba,$5d,$5d,$e7,$5d,
$19,$19,$2b,$32,$19,$19,$2b,$19,$73,$73,$95,$e6,$73,$73,$95,$73,
$60,$60,$a0,$c0,$60,$60,$a0,$60,$81,$81,$98,$19,$81,$81,$98,$81,
$4f,$4f,$d1,$9e,$4f,$4f,$d1,$4f,$dc,$dc,$7f,$a3,$dc,$dc,$7f,$dc,
$22,$22,$66,$44,$22,$22,$66,$22,$2a,$2a,$7e,$54,$2a,$2a,$7e,$2a,
$90,$90,$ab,$3b,$90,$90,$ab,$90,$88,$88,$83,$0b,$88,$88,$83,$88,
$46,$46,$ca,$8c,$46,$46,$ca,$46,$ee,$ee,$29,$c7,$ee,$ee,$29,$ee,
$b8,$b8,$d3,$6b,$b8,$b8,$d3,$b8,$14,$14,$3c,$28,$14,$14,$3c,$14,
$de,$de,$79,$a7,$de,$de,$79,$de,$5e,$5e,$e2,$bc,$5e,$5e,$e2,$5e,
$0b,$0b,$1d,$16,$0b,$0b,$1d,$0b,$db,$db,$76,$ad,$db,$db,$76,$db,
$e0,$e0,$3b,$db,$e0,$e0,$3b,$e0,$32,$32,$56,$64,$32,$32,$56,$32,
$3a,$3a,$4e,$74,$3a,$3a,$4e,$3a,$0a,$0a,$1e,$14,$0a,$0a,$1e,$0a,
$49,$49,$db,$92,$49,$49,$db,$49,$06,$06,$0a,$0c,$06,$06,$0a,$06,
$24,$24,$6c,$48,$24,$24,$6c,$24,$5c,$5c,$e4,$b8,$5c,$5c,$e4,$5c,
$c2,$c2,$5d,$9f,$c2,$c2,$5d,$c2,$d3,$d3,$6e,$bd,$d3,$d3,$6e,$d3,
$ac,$ac,$ef,$43,$ac,$ac,$ef,$ac,$62,$62,$a6,$c4,$62,$62,$a6,$62,
$91,$91,$a8,$39,$91,$91,$a8,$91,$95,$95,$a4,$31,$95,$95,$a4,$95,
$e4,$e4,$37,$d3,$e4,$e4,$37,$e4,$79,$79,$8b,$f2,$79,$79,$8b,$79,
$e7,$e7,$32,$d5,$e7,$e7,$32,$e7,$c8,$c8,$43,$8b,$c8,$c8,$43,$c8,
$37,$37,$59,$6e,$37,$37,$59,$37,$6d,$6d,$b7,$da,$6d,$6d,$b7,$6d,
$8d,$8d,$8c,$01,$8d,$8d,$8c,$8d,$d5,$d5,$64,$b1,$d5,$d5,$64,$d5,
$4e,$4e,$d2,$9c,$4e,$4e,$d2,$4e,$a9,$a9,$e0,$49,$a9,$a9,$e0,$a9,
$6c,$6c,$b4,$d8,$6c,$6c,$b4,$6c,$56,$56,$fa,$ac,$56,$56,$fa,$56,
$f4,$f4,$07,$f3,$f4,$f4,$07,$f4,$ea,$ea,$25,$cf,$ea,$ea,$25,$ea,
$65,$65,$af,$ca,$65,$65,$af,$65,$7a,$7a,$8e,$f4,$7a,$7a,$8e,$7a,
$ae,$ae,$e9,$47,$ae,$ae,$e9,$ae,$08,$08,$18,$10,$08,$08,$18,$08,
$ba,$ba,$d5,$6f,$ba,$ba,$d5,$ba,$78,$78,$88,$f0,$78,$78,$88,$78,
$25,$25,$6f,$4a,$25,$25,$6f,$25,$2e,$2e,$72,$5c,$2e,$2e,$72,$2e,
$1c,$1c,$24,$38,$1c,$1c,$24,$1c,$a6,$a6,$f1,$57,$a6,$a6,$f1,$a6,
$b4,$b4,$c7,$73,$b4,$b4,$c7,$b4,$c6,$c6,$51,$97,$c6,$c6,$51,$c6,
$e8,$e8,$23,$cb,$e8,$e8,$23,$e8,$dd,$dd,$7c,$a1,$dd,$dd,$7c,$dd,
$74,$74,$9c,$e8,$74,$74,$9c,$74,$1f,$1f,$21,$3e,$1f,$1f,$21,$1f,
$4b,$4b,$dd,$96,$4b,$4b,$dd,$4b,$bd,$bd,$dc,$61,$bd,$bd,$dc,$bd,
$8b,$8b,$86,$0d,$8b,$8b,$86,$8b,$8a,$8a,$85,$0f,$8a,$8a,$85,$8a,
$70,$70,$90,$e0,$70,$70,$90,$70,$3e,$3e,$42,$7c,$3e,$3e,$42,$3e,
$b5,$b5,$c4,$71,$b5,$b5,$c4,$b5,$66,$66,$aa,$cc,$66,$66,$aa,$66,
$48,$48,$d8,$90,$48,$48,$d8,$48,$03,$03,$05,$06,$03,$03,$05,$03,
$f6,$f6,$01,$f7,$f6,$f6,$01,$f6,$0e,$0e,$12,$1c,$0e,$0e,$12,$0e,
$61,$61,$a3,$c2,$61,$61,$a3,$61,$35,$35,$5f,$6a,$35,$35,$5f,$35,
$57,$57,$f9,$ae,$57,$57,$f9,$57,$b9,$b9,$d0,$69,$b9,$b9,$d0,$b9,
$86,$86,$91,$17,$86,$86,$91,$86,$c1,$c1,$58,$99,$c1,$c1,$58,$c1,
$1d,$1d,$27,$3a,$1d,$1d,$27,$1d,$9e,$9e,$b9,$27,$9e,$9e,$b9,$9e,
$e1,$e1,$38,$d9,$e1,$e1,$38,$e1,$f8,$f8,$13,$eb,$f8,$f8,$13,$f8,
$98,$98,$b3,$2b,$98,$98,$b3,$98,$11,$11,$33,$22,$11,$11,$33,$11,
$69,$69,$bb,$d2,$69,$69,$bb,$69,$d9,$d9,$70,$a9,$d9,$d9,$70,$d9,
$8e,$8e,$89,$07,$8e,$8e,$89,$8e,$94,$94,$a7,$33,$94,$94,$a7,$94,
$9b,$9b,$b6,$2d,$9b,$9b,$b6,$9b,$1e,$1e,$22,$3c,$1e,$1e,$22,$1e,
$87,$87,$92,$15,$87,$87,$92,$87,$e9,$e9,$20,$c9,$e9,$e9,$20,$e9,
$ce,$ce,$49,$87,$ce,$ce,$49,$ce,$55,$55,$ff,$aa,$55,$55,$ff,$55,
$28,$28,$78,$50,$28,$28,$78,$28,$df,$df,$7a,$a5,$df,$df,$7a,$df,
$8c,$8c,$8f,$03,$8c,$8c,$8f,$8c,$a1,$a1,$f8,$59,$a1,$a1,$f8,$a1,
$89,$89,$80,$09,$89,$89,$80,$89,$0d,$0d,$17,$1a,$0d,$0d,$17,$0d,
$bf,$bf,$da,$65,$bf,$bf,$da,$bf,$e6,$e6,$31,$d7,$e6,$e6,$31,$e6,
$42,$42,$c6,$84,$42,$42,$c6,$42,$68,$68,$b8,$d0,$68,$68,$b8,$68,
$41,$41,$c3,$82,$41,$41,$c3,$41,$99,$99,$b0,$29,$99,$99,$b0,$99,
$2d,$2d,$77,$5a,$2d,$2d,$77,$2d,$0f,$0f,$11,$1e,$0f,$0f,$11,$0f,
$b0,$b0,$cb,$7b,$b0,$b0,$cb,$b0,$54,$54,$fc,$a8,$54,$54,$fc,$54,
$bb,$bb,$d6,$6d,$bb,$bb,$d6,$bb,$16,$16,$3a,$2c,$16,$16,$3a,$16);
var
Te: array[byte] of TEU absolute TCe;
{$ifdef StrictLong}
{$warnings on}
{$ifdef RangeChecks_on}
{$R+}
{$endif}
{$endif}

View File

@ -0,0 +1,73 @@
(*************************************************************************
Include file for AES_ENCR.PAS - AES_Encrypt for Pascal16/Compressed tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version for compressed tables
0.11 13.07.06 we Uses TCe box byte instead of SBox
0.12 15.11.08 we Use Ptr2Inc from BTypes
**************************************************************************)
(**** (C) Copyright 2002-2008 Wolfgang Ehrhardt -- see copying_we.txt ****)
{Normally used for TP5/5.5 (and during development BP7)}
{---------------------------------------------------------------------------}
procedure AES_Encrypt(var ctx: TAESContext; {$ifdef CONST} const {$else} var {$endif} BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
label done;
var
pK: PWA4; {pointer to loop round key}
r: integer; {round loop counter}
t,s: TAESBlock;
begin
{Setup key pointer}
pK := PWA4(@ctx.RK);
{Initialize with input block}
TWA4(s)[0] := TWA4(BI)[0] xor pK^[0];
TWA4(s)[1] := TWA4(BI)[1] xor pK^[1];
TWA4(s)[2] := TWA4(BI)[2] xor pK^[2];
TWA4(s)[3] := TWA4(BI)[3] xor pK^[3];
inc(Ptr2Inc(pK), 4*sizeof(longint));
r := 1;
while true do begin
TWA4(t)[0] := Te[s[0*4+0]].E0.L xor Te[s[1*4+1]].E1.L xor Te[s[2*4+2]].E2.L xor Te[s[3*4+3]].E3.L xor pK^[0];
TWA4(t)[1] := Te[s[1*4+0]].E0.L xor Te[s[2*4+1]].E1.L xor Te[s[3*4+2]].E2.L xor Te[s[0*4+3]].E3.L xor pK^[1];
TWA4(t)[2] := Te[s[2*4+0]].E0.L xor Te[s[3*4+1]].E1.L xor Te[s[0*4+2]].E2.L xor Te[s[1*4+3]].E3.L xor pK^[2];
TWA4(t)[3] := Te[s[3*4+0]].E0.L xor Te[s[0*4+1]].E1.L xor Te[s[1*4+2]].E2.L xor Te[s[2*4+3]].E3.L xor pK^[3];
inc(Ptr2Inc(pK), 4*sizeof(longint));
inc(r);
if r>=ctx.rounds then goto done;
TWA4(s)[0] := Te[t[0*4+0]].E0.L xor Te[t[1*4+1]].E1.L xor Te[t[2*4+2]].E2.L xor Te[t[3*4+3]].E3.L xor pK^[0];
TWA4(s)[1] := Te[t[1*4+0]].E0.L xor Te[t[2*4+1]].E1.L xor Te[t[3*4+2]].E2.L xor Te[t[0*4+3]].E3.L xor pK^[1];
TWA4(s)[2] := Te[t[2*4+0]].E0.L xor Te[t[3*4+1]].E1.L xor Te[t[0*4+2]].E2.L xor Te[t[1*4+3]].E3.L xor pK^[2];
TWA4(s)[3] := Te[t[3*4+0]].E0.L xor Te[t[0*4+1]].E1.L xor Te[t[1*4+2]].E2.L xor Te[t[2*4+3]].E3.L xor pK^[3];
inc(Ptr2Inc(pK), 4*sizeof(longint));
inc(r);
end;
done:
s[00] := Te[t[0*4+0]].E0.box;
s[01] := Te[t[1*4+1]].E0.box;
s[02] := Te[t[2*4+2]].E0.box;
s[03] := Te[t[3*4+3]].E0.box;
s[04] := Te[t[1*4+0]].E0.box;
s[05] := Te[t[2*4+1]].E0.box;
s[06] := Te[t[3*4+2]].E0.box;
s[07] := Te[t[0*4+3]].E0.box;
s[08] := Te[t[2*4+0]].E0.box;
s[09] := Te[t[3*4+1]].E0.box;
s[10] := Te[t[0*4+2]].E0.box;
s[11] := Te[t[1*4+3]].E0.box;
s[12] := Te[t[3*4+0]].E0.box;
s[13] := Te[t[0*4+1]].E0.box;
s[14] := Te[t[1*4+2]].E0.box;
s[15] := Te[t[2*4+3]].E0.box;
TWA4(BO)[0] := TWA4(s)[0] xor pK^[0];
TWA4(BO)[1] := TWA4(s)[1] xor pK^[1];
TWA4(BO)[2] := TWA4(s)[2] xor pK^[2];
TWA4(BO)[3] := TWA4(s)[3] xor pK^[3];
end;

View File

@ -0,0 +1,62 @@
(*************************************************************************
Include file for AES_ENCR.PAS - AES_Encrypt for BIT32/Compressed tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version for compressed tables
0.11 09.07.06 we Removed AES_LONGBOX code
0.12 13.07.06 we Uses TCe box byte instead of SBox
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{---------------------------------------------------------------------------}
procedure AES_Encrypt(var ctx: TAESContext; const BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
var
r: integer; {round loop countdown counter}
pK: PWA4; {pointer to loop round key }
s3,s0,s1,s2: longint; {TAESBlock s as separate variables}
t: TWA4;
begin
{Setup key pointer}
pK := PWA4(@ctx.RK);
{Initialize with input block}
s0 := TWA4(BI)[0] xor pK^[0];
s1 := TWA4(BI)[1] xor pK^[1];
s2 := TWA4(BI)[2] xor pK^[2];
s3 := TWA4(BI)[3] xor pK^[3];
inc(pK);
{perform encryption rounds}
for r:=1 to ctx.Rounds-1 do begin
t[0] := Te[s0 and $ff].E0.L xor Te[s1 shr 8 and $ff].E1.L xor Te[s2 shr 16 and $ff].E2.L xor Te[s3 shr 24].E3.L xor pK^[0];
t[1] := Te[s1 and $ff].E0.L xor Te[s2 shr 8 and $ff].E1.L xor Te[s3 shr 16 and $ff].E2.L xor Te[s0 shr 24].E3.L xor pK^[1];
t[2] := Te[s2 and $ff].E0.L xor Te[s3 shr 8 and $ff].E1.L xor Te[s0 shr 16 and $ff].E2.L xor Te[s1 shr 24].E3.L xor pK^[2];
s3 := Te[s3 and $ff].E0.L xor Te[s0 shr 8 and $ff].E1.L xor Te[s1 shr 16 and $ff].E2.L xor Te[s2 shr 24].E3.L xor pK^[3];
s0 := t[0];
s1 := t[1];
s2 := t[2];
inc(pK);
end;
{Uses Sbox byte from Te and shl, needs type cast longint() for 16 bit compilers}
TWA4(BO)[0] := (longint(Te[s0 and $ff].E0.box) xor
longint(Te[s1 shr 8 and $ff].E0.box) shl 8 xor
longint(Te[s2 shr 16 and $ff].E0.box) shl 16 xor
longint(Te[s3 shr 24 ].E0.box) shl 24 ) xor pK^[0];
TWA4(BO)[1] := (longint(Te[s1 and $ff].E0.box) xor
longint(Te[s2 shr 8 and $ff].E0.box) shl 8 xor
longint(Te[s3 shr 16 and $ff].E0.box) shl 16 xor
longint(Te[s0 shr 24 ].E0.box) shl 24 ) xor pK^[1];
TWA4(BO)[2] := (longint(Te[s2 and $ff].E0.box) xor
longint(Te[s3 shr 8 and $ff].E0.box) shl 8 xor
longint(Te[s0 shr 16 and $ff].E0.box) shl 16 xor
longint(Te[s1 shr 24 ].E0.box) shl 24 ) xor pK^[2];
TWA4(BO)[3] := (longint(Te[s3 and $ff].E0.box) xor
longint(Te[s0 shr 8 and $ff].E0.box) shl 8 xor
longint(Te[s1 shr 16 and $ff].E0.box) shl 16 xor
longint(Te[s2 shr 24 ].E0.box) shl 24 ) xor pK^[3];
end;

View File

@ -0,0 +1,318 @@
(*************************************************************************
Include file for AES_ENCR.PAS - AES_Encrypt for BASM16/Full tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version from AES_ENCR.PAS
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{16 bit BASM used for TP6, BP7, Delphi1}
{---------------------------------------------------------------------------}
procedure AES_Encrypt(var ctx: TAESContext; {$ifdef CONST} const {$else} var {$endif} BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
var
s,t: TAESBlock;
rnd: integer;
pK: pointer;
begin
rnd := ctx.rounds;
pK := @ctx.RK;
asm
db $66; pusha
{AES_XorBlock(BI, ctx.RK[0], s);}
les si,[BI]
db $66; mov ax,es:[si]
db $66; mov bx,es:[si+4]
db $66; mov cx,es:[si+8]
db $66; mov dx,es:[si+12]
les di,[pK]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
db $66; mov word ptr [s],ax
db $66; mov word ptr [s+4],bx
db $66; mov word ptr [s+8],cx
db $66; mov word ptr [s+12],dx
add di,16 {di->ctx.RK[1]}
mov dx,[rnd]
mov cx,1
{ *Note* in the following round loop }
{ op eax, mem[4*bx] is calculated as }
{ lea esi, [ebx + 2*ebx] }
{ op eax, mem[ebx+esi] }
{ lea esi,[ebx+2*ebx] = db $66,$67,$8D,$34,$5B; }
db $66; sub bx,bx {clear ebx}
@@1:
{TWA4(t)[0] := Te0[s[0*4+0]] xor Te1[s[1*4+1]] xor Te2[s[2*4+2]] xor Te3[s[3*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr s[0*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Te0[bx+si]
mov bl,byte ptr s[1*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te1[bx+si]
mov bl,byte ptr s[2*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te2[bx+si]
mov bl,byte ptr s[3*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te3[bx+si]
db $66; xor ax,es:[di]
db $66; mov word ptr t[0],ax
{TWA4(t)[1] := Te0[s[1*4+0]] xor Te1[s[2*4+1]] xor Te2[s[3*4+2]] xor Te3[s[0*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr s[1*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Te0[bx+si]
mov bl,byte ptr s[2*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te1[bx+si]
mov bl,byte ptr s[3*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te2[bx+si]
mov bl,byte ptr s[0*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te3[bx+si]
db $66; xor ax,es:[di+4]
db $66; mov word ptr t[4],ax
{TWA4(t)[2] := Te0[s[2*4+0]] xor Te1[s[3*4+1]] xor Te2[s[0*4+2]] xor Te3[s[1*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr s[2*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Te0[bx+si]
mov bl,byte ptr s[3*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te1[bx+si]
mov bl,byte ptr s[0*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te2[bx+si]
mov bl,byte ptr s[1*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te3[bx+si]
db $66; xor ax,es:[di+8]
db $66; mov word ptr t[8],ax
{TWA4(t)[3] := Te0[s[3*4+0]] xor Te1[s[0*4+1]] xor Te2[s[1*4+2]] xor Te3[s[2*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr s[3*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Te0[bx+si]
mov bl,byte ptr s[0*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te1[bx+si]
mov bl,byte ptr s[1*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te2[bx+si]
mov bl,byte ptr s[2*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te3[bx+si]
db $66; xor ax,es:[di+12]
db $66; mov word ptr t[12],ax
{if r>=ctx.rounds then break;}
inc cx
cmp cx,dx
jae @@2
{TWA4(s)[0] := Te0[t[0*4+0]] xor Te1[t[1*4+1]] xor Te2[t[2*4+2]] xor Te3[t[3*4+3]] xor TWA4(ctx.RK[r])[0];}
mov bl,byte ptr t[0*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Te0[bx+si]
mov bl,byte ptr t[1*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te1[bx+si]
mov bl,byte ptr t[2*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te2[bx+si]
mov bl,byte ptr t[3*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te3[bx+si]
db $66; xor ax,es:[di+16]
db $66; mov word ptr s[0],ax
{TWA4(s)[1] := Te0[t[1*4+0]] xor Te1[t[2*4+1]] xor Te2[t[3*4+2]] xor Te3[t[0*4+3]] xor TWA4(ctx.RK[r])[1];}
mov bl,byte ptr t[1*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Te0[bx+si]
mov bl,byte ptr t[2*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te1[bx+si]
mov bl,byte ptr t[3*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te2[bx+si]
mov bl,byte ptr t[0*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te3[bx+si]
db $66; xor ax,es:[di+20]
db $66; mov word ptr s[4],ax
{TWA4(s)[2] := Te0[t[2*4+0]] xor Te1[t[3*4+1]] xor Te2[t[0*4+2]] xor Te3[t[1*4+3]] xor TWA4(ctx.RK[r])[2];}
mov bl,byte ptr t[2*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Te0[bx+si]
mov bl,byte ptr t[3*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te1[bx+si]
mov bl,byte ptr t[0*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te2[bx+si]
mov bl,byte ptr t[1*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te3[bx+si]
db $66; xor ax,es:[di+24]
db $66; mov word ptr s[8],ax
{TWA4(s)[3] := Te0[t[3*4+0]] xor Te1[t[0*4+1]] xor Te2[t[1*4+2]] xor Te3[t[2*4+3]] xor TWA4(ctx.RK[r])[3];}
mov bl,byte ptr t[3*4+0]
db $66,$67,$8D,$34,$5B;
db $66; mov ax,word ptr Te0[bx+si]
mov bl,byte ptr t[0*4+1]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te1[bx+si]
mov bl,byte ptr t[1*4+2]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te2[bx+si]
mov bl,byte ptr t[2*4+3]
db $66,$67,$8D,$34,$5B;
db $66; xor ax,word ptr Te3[bx+si]
db $66; xor ax,es:[di+28]
add di,32
db $66; mov word ptr s[12],ax
inc cx
jmp @@1
@@2: add di,16 {di -> ctx.RK[ctx.rounds]}
{Last round uses SBox}
sub bx,bx
mov bl, byte ptr t[0*4+0]
mov al, byte ptr SBox[bx]
mov byte ptr s[0],al
mov bl, byte ptr t[1*4+1]
mov al, byte ptr SBox[bx]
mov byte ptr s[1],al
mov bl, byte ptr t[2*4+2]
mov al, byte ptr SBox[bx]
mov byte ptr s[2],al
mov bl, byte ptr t[3*4+3]
mov al, byte ptr SBox[bx]
mov byte ptr s[3],al
mov bl, byte ptr t[1*4+0]
mov al, byte ptr SBox[bx]
mov byte ptr s[4],al
mov bl, byte ptr t[2*4+1]
mov al, byte ptr SBox[bx]
mov byte ptr s[5],al
mov bl, byte ptr t[3*4+2]
mov al, byte ptr SBox[bx]
mov byte ptr s[6],al
mov bl, byte ptr t[0*4+3]
mov al, byte ptr SBox[bx]
mov byte ptr s[7],al
mov bl, byte ptr t[2*4+0]
mov al, byte ptr SBox[bx]
mov byte ptr s[8],al
mov bl, byte ptr t[3*4+1]
mov al, byte ptr SBox[bx]
mov byte ptr s[9],al
mov bl, byte ptr t[0*4+2]
mov al, byte ptr SBox[bx]
mov byte ptr s[10],al
mov bl, byte ptr t[1*4+3]
mov al, byte ptr SBox[bx]
mov byte ptr s[11],al
mov bl, byte ptr t[3*4+0]
mov al, byte ptr SBox[bx]
mov byte ptr s[12],al
mov bl, byte ptr t[0*4+1]
mov al, byte ptr SBox[bx]
mov byte ptr s[13],al
mov bl, byte ptr t[1*4+2]
mov al, byte ptr SBox[bx]
mov byte ptr s[14],al
mov bl, byte ptr t[2*4+3]
mov al, byte ptr SBox[bx]
mov byte ptr s[15],al
{AES_XorBlock(s, ctx.RK[rnd], BO)}
db $66; mov ax,word ptr [s]
db $66; mov bx,word ptr [s+4]
db $66; mov cx,word ptr [s+8]
db $66; mov dx,word ptr [s+12]
db $66; xor ax,es:[di]
db $66; xor bx,es:[di+4]
db $66; xor cx,es:[di+8]
db $66; xor dx,es:[di+12]
les si,[BO]
db $66; mov es:[si],ax
db $66; mov es:[si+4],bx
db $66; mov es:[si+8],cx
db $66; mov es:[si+12],dx
db $66; popa
end;
end;

View File

@ -0,0 +1,207 @@
(*************************************************************************
Include file for AES_ENCR.PAS - Full tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version from AES_ENCR.PAS
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{$ifdef StrictLong}
{$warnings off}
{$R-} {avoid D9+ errors!}
{$endif}
const
Te0: array[byte] of longint =
($a56363c6, $847c7cf8, $997777ee, $8d7b7bf6, $0df2f2ff, $bd6b6bd6, $b16f6fde, $54c5c591,
$50303060, $03010102, $a96767ce, $7d2b2b56, $19fefee7, $62d7d7b5, $e6abab4d, $9a7676ec,
$45caca8f, $9d82821f, $40c9c989, $877d7dfa, $15fafaef, $eb5959b2, $c947478e, $0bf0f0fb,
$ecadad41, $67d4d4b3, $fda2a25f, $eaafaf45, $bf9c9c23, $f7a4a453, $967272e4, $5bc0c09b,
$c2b7b775, $1cfdfde1, $ae93933d, $6a26264c, $5a36366c, $413f3f7e, $02f7f7f5, $4fcccc83,
$5c343468, $f4a5a551, $34e5e5d1, $08f1f1f9, $937171e2, $73d8d8ab, $53313162, $3f15152a,
$0c040408, $52c7c795, $65232346, $5ec3c39d, $28181830, $a1969637, $0f05050a, $b59a9a2f,
$0907070e, $36121224, $9b80801b, $3de2e2df, $26ebebcd, $6927274e, $cdb2b27f, $9f7575ea,
$1b090912, $9e83831d, $742c2c58, $2e1a1a34, $2d1b1b36, $b26e6edc, $ee5a5ab4, $fba0a05b,
$f65252a4, $4d3b3b76, $61d6d6b7, $ceb3b37d, $7b292952, $3ee3e3dd, $712f2f5e, $97848413,
$f55353a6, $68d1d1b9, $00000000, $2cededc1, $60202040, $1ffcfce3, $c8b1b179, $ed5b5bb6,
$be6a6ad4, $46cbcb8d, $d9bebe67, $4b393972, $de4a4a94, $d44c4c98, $e85858b0, $4acfcf85,
$6bd0d0bb, $2aefefc5, $e5aaaa4f, $16fbfbed, $c5434386, $d74d4d9a, $55333366, $94858511,
$cf45458a, $10f9f9e9, $06020204, $817f7ffe, $f05050a0, $443c3c78, $ba9f9f25, $e3a8a84b,
$f35151a2, $fea3a35d, $c0404080, $8a8f8f05, $ad92923f, $bc9d9d21, $48383870, $04f5f5f1,
$dfbcbc63, $c1b6b677, $75dadaaf, $63212142, $30101020, $1affffe5, $0ef3f3fd, $6dd2d2bf,
$4ccdcd81, $140c0c18, $35131326, $2fececc3, $e15f5fbe, $a2979735, $cc444488, $3917172e,
$57c4c493, $f2a7a755, $827e7efc, $473d3d7a, $ac6464c8, $e75d5dba, $2b191932, $957373e6,
$a06060c0, $98818119, $d14f4f9e, $7fdcdca3, $66222244, $7e2a2a54, $ab90903b, $8388880b,
$ca46468c, $29eeeec7, $d3b8b86b, $3c141428, $79dedea7, $e25e5ebc, $1d0b0b16, $76dbdbad,
$3be0e0db, $56323264, $4e3a3a74, $1e0a0a14, $db494992, $0a06060c, $6c242448, $e45c5cb8,
$5dc2c29f, $6ed3d3bd, $efacac43, $a66262c4, $a8919139, $a4959531, $37e4e4d3, $8b7979f2,
$32e7e7d5, $43c8c88b, $5937376e, $b76d6dda, $8c8d8d01, $64d5d5b1, $d24e4e9c, $e0a9a949,
$b46c6cd8, $fa5656ac, $07f4f4f3, $25eaeacf, $af6565ca, $8e7a7af4, $e9aeae47, $18080810,
$d5baba6f, $887878f0, $6f25254a, $722e2e5c, $241c1c38, $f1a6a657, $c7b4b473, $51c6c697,
$23e8e8cb, $7cdddda1, $9c7474e8, $211f1f3e, $dd4b4b96, $dcbdbd61, $868b8b0d, $858a8a0f,
$907070e0, $423e3e7c, $c4b5b571, $aa6666cc, $d8484890, $05030306, $01f6f6f7, $120e0e1c,
$a36161c2, $5f35356a, $f95757ae, $d0b9b969, $91868617, $58c1c199, $271d1d3a, $b99e9e27,
$38e1e1d9, $13f8f8eb, $b398982b, $33111122, $bb6969d2, $70d9d9a9, $898e8e07, $a7949433,
$b69b9b2d, $221e1e3c, $92878715, $20e9e9c9, $49cece87, $ff5555aa, $78282850, $7adfdfa5,
$8f8c8c03, $f8a1a159, $80898909, $170d0d1a, $dabfbf65, $31e6e6d7, $c6424284, $b86868d0,
$c3414182, $b0999929, $772d2d5a, $110f0f1e, $cbb0b07b, $fc5454a8, $d6bbbb6d, $3a16162c);
Te1: array[byte] of longint =
($6363c6a5, $7c7cf884, $7777ee99, $7b7bf68d, $f2f2ff0d, $6b6bd6bd, $6f6fdeb1, $c5c59154,
$30306050, $01010203, $6767cea9, $2b2b567d, $fefee719, $d7d7b562, $abab4de6, $7676ec9a,
$caca8f45, $82821f9d, $c9c98940, $7d7dfa87, $fafaef15, $5959b2eb, $47478ec9, $f0f0fb0b,
$adad41ec, $d4d4b367, $a2a25ffd, $afaf45ea, $9c9c23bf, $a4a453f7, $7272e496, $c0c09b5b,
$b7b775c2, $fdfde11c, $93933dae, $26264c6a, $36366c5a, $3f3f7e41, $f7f7f502, $cccc834f,
$3434685c, $a5a551f4, $e5e5d134, $f1f1f908, $7171e293, $d8d8ab73, $31316253, $15152a3f,
$0404080c, $c7c79552, $23234665, $c3c39d5e, $18183028, $969637a1, $05050a0f, $9a9a2fb5,
$07070e09, $12122436, $80801b9b, $e2e2df3d, $ebebcd26, $27274e69, $b2b27fcd, $7575ea9f,
$0909121b, $83831d9e, $2c2c5874, $1a1a342e, $1b1b362d, $6e6edcb2, $5a5ab4ee, $a0a05bfb,
$5252a4f6, $3b3b764d, $d6d6b761, $b3b37dce, $2929527b, $e3e3dd3e, $2f2f5e71, $84841397,
$5353a6f5, $d1d1b968, $00000000, $ededc12c, $20204060, $fcfce31f, $b1b179c8, $5b5bb6ed,
$6a6ad4be, $cbcb8d46, $bebe67d9, $3939724b, $4a4a94de, $4c4c98d4, $5858b0e8, $cfcf854a,
$d0d0bb6b, $efefc52a, $aaaa4fe5, $fbfbed16, $434386c5, $4d4d9ad7, $33336655, $85851194,
$45458acf, $f9f9e910, $02020406, $7f7ffe81, $5050a0f0, $3c3c7844, $9f9f25ba, $a8a84be3,
$5151a2f3, $a3a35dfe, $404080c0, $8f8f058a, $92923fad, $9d9d21bc, $38387048, $f5f5f104,
$bcbc63df, $b6b677c1, $dadaaf75, $21214263, $10102030, $ffffe51a, $f3f3fd0e, $d2d2bf6d,
$cdcd814c, $0c0c1814, $13132635, $ececc32f, $5f5fbee1, $979735a2, $444488cc, $17172e39,
$c4c49357, $a7a755f2, $7e7efc82, $3d3d7a47, $6464c8ac, $5d5dbae7, $1919322b, $7373e695,
$6060c0a0, $81811998, $4f4f9ed1, $dcdca37f, $22224466, $2a2a547e, $90903bab, $88880b83,
$46468cca, $eeeec729, $b8b86bd3, $1414283c, $dedea779, $5e5ebce2, $0b0b161d, $dbdbad76,
$e0e0db3b, $32326456, $3a3a744e, $0a0a141e, $494992db, $06060c0a, $2424486c, $5c5cb8e4,
$c2c29f5d, $d3d3bd6e, $acac43ef, $6262c4a6, $919139a8, $959531a4, $e4e4d337, $7979f28b,
$e7e7d532, $c8c88b43, $37376e59, $6d6ddab7, $8d8d018c, $d5d5b164, $4e4e9cd2, $a9a949e0,
$6c6cd8b4, $5656acfa, $f4f4f307, $eaeacf25, $6565caaf, $7a7af48e, $aeae47e9, $08081018,
$baba6fd5, $7878f088, $25254a6f, $2e2e5c72, $1c1c3824, $a6a657f1, $b4b473c7, $c6c69751,
$e8e8cb23, $dddda17c, $7474e89c, $1f1f3e21, $4b4b96dd, $bdbd61dc, $8b8b0d86, $8a8a0f85,
$7070e090, $3e3e7c42, $b5b571c4, $6666ccaa, $484890d8, $03030605, $f6f6f701, $0e0e1c12,
$6161c2a3, $35356a5f, $5757aef9, $b9b969d0, $86861791, $c1c19958, $1d1d3a27, $9e9e27b9,
$e1e1d938, $f8f8eb13, $98982bb3, $11112233, $6969d2bb, $d9d9a970, $8e8e0789, $949433a7,
$9b9b2db6, $1e1e3c22, $87871592, $e9e9c920, $cece8749, $5555aaff, $28285078, $dfdfa57a,
$8c8c038f, $a1a159f8, $89890980, $0d0d1a17, $bfbf65da, $e6e6d731, $424284c6, $6868d0b8,
$414182c3, $999929b0, $2d2d5a77, $0f0f1e11, $b0b07bcb, $5454a8fc, $bbbb6dd6, $16162c3a);
Te2: array[byte] of longint =
($63c6a563, $7cf8847c, $77ee9977, $7bf68d7b, $f2ff0df2, $6bd6bd6b, $6fdeb16f, $c59154c5,
$30605030, $01020301, $67cea967, $2b567d2b, $fee719fe, $d7b562d7, $ab4de6ab, $76ec9a76,
$ca8f45ca, $821f9d82, $c98940c9, $7dfa877d, $faef15fa, $59b2eb59, $478ec947, $f0fb0bf0,
$ad41ecad, $d4b367d4, $a25ffda2, $af45eaaf, $9c23bf9c, $a453f7a4, $72e49672, $c09b5bc0,
$b775c2b7, $fde11cfd, $933dae93, $264c6a26, $366c5a36, $3f7e413f, $f7f502f7, $cc834fcc,
$34685c34, $a551f4a5, $e5d134e5, $f1f908f1, $71e29371, $d8ab73d8, $31625331, $152a3f15,
$04080c04, $c79552c7, $23466523, $c39d5ec3, $18302818, $9637a196, $050a0f05, $9a2fb59a,
$070e0907, $12243612, $801b9b80, $e2df3de2, $ebcd26eb, $274e6927, $b27fcdb2, $75ea9f75,
$09121b09, $831d9e83, $2c58742c, $1a342e1a, $1b362d1b, $6edcb26e, $5ab4ee5a, $a05bfba0,
$52a4f652, $3b764d3b, $d6b761d6, $b37dceb3, $29527b29, $e3dd3ee3, $2f5e712f, $84139784,
$53a6f553, $d1b968d1, $00000000, $edc12ced, $20406020, $fce31ffc, $b179c8b1, $5bb6ed5b,
$6ad4be6a, $cb8d46cb, $be67d9be, $39724b39, $4a94de4a, $4c98d44c, $58b0e858, $cf854acf,
$d0bb6bd0, $efc52aef, $aa4fe5aa, $fbed16fb, $4386c543, $4d9ad74d, $33665533, $85119485,
$458acf45, $f9e910f9, $02040602, $7ffe817f, $50a0f050, $3c78443c, $9f25ba9f, $a84be3a8,
$51a2f351, $a35dfea3, $4080c040, $8f058a8f, $923fad92, $9d21bc9d, $38704838, $f5f104f5,
$bc63dfbc, $b677c1b6, $daaf75da, $21426321, $10203010, $ffe51aff, $f3fd0ef3, $d2bf6dd2,
$cd814ccd, $0c18140c, $13263513, $ecc32fec, $5fbee15f, $9735a297, $4488cc44, $172e3917,
$c49357c4, $a755f2a7, $7efc827e, $3d7a473d, $64c8ac64, $5dbae75d, $19322b19, $73e69573,
$60c0a060, $81199881, $4f9ed14f, $dca37fdc, $22446622, $2a547e2a, $903bab90, $880b8388,
$468cca46, $eec729ee, $b86bd3b8, $14283c14, $dea779de, $5ebce25e, $0b161d0b, $dbad76db,
$e0db3be0, $32645632, $3a744e3a, $0a141e0a, $4992db49, $060c0a06, $24486c24, $5cb8e45c,
$c29f5dc2, $d3bd6ed3, $ac43efac, $62c4a662, $9139a891, $9531a495, $e4d337e4, $79f28b79,
$e7d532e7, $c88b43c8, $376e5937, $6ddab76d, $8d018c8d, $d5b164d5, $4e9cd24e, $a949e0a9,
$6cd8b46c, $56acfa56, $f4f307f4, $eacf25ea, $65caaf65, $7af48e7a, $ae47e9ae, $08101808,
$ba6fd5ba, $78f08878, $254a6f25, $2e5c722e, $1c38241c, $a657f1a6, $b473c7b4, $c69751c6,
$e8cb23e8, $dda17cdd, $74e89c74, $1f3e211f, $4b96dd4b, $bd61dcbd, $8b0d868b, $8a0f858a,
$70e09070, $3e7c423e, $b571c4b5, $66ccaa66, $4890d848, $03060503, $f6f701f6, $0e1c120e,
$61c2a361, $356a5f35, $57aef957, $b969d0b9, $86179186, $c19958c1, $1d3a271d, $9e27b99e,
$e1d938e1, $f8eb13f8, $982bb398, $11223311, $69d2bb69, $d9a970d9, $8e07898e, $9433a794,
$9b2db69b, $1e3c221e, $87159287, $e9c920e9, $ce8749ce, $55aaff55, $28507828, $dfa57adf,
$8c038f8c, $a159f8a1, $89098089, $0d1a170d, $bf65dabf, $e6d731e6, $4284c642, $68d0b868,
$4182c341, $9929b099, $2d5a772d, $0f1e110f, $b07bcbb0, $54a8fc54, $bb6dd6bb, $162c3a16);
Te3: array[byte] of longint =
($c6a56363, $f8847c7c, $ee997777, $f68d7b7b, $ff0df2f2, $d6bd6b6b, $deb16f6f, $9154c5c5,
$60503030, $02030101, $cea96767, $567d2b2b, $e719fefe, $b562d7d7, $4de6abab, $ec9a7676,
$8f45caca, $1f9d8282, $8940c9c9, $fa877d7d, $ef15fafa, $b2eb5959, $8ec94747, $fb0bf0f0,
$41ecadad, $b367d4d4, $5ffda2a2, $45eaafaf, $23bf9c9c, $53f7a4a4, $e4967272, $9b5bc0c0,
$75c2b7b7, $e11cfdfd, $3dae9393, $4c6a2626, $6c5a3636, $7e413f3f, $f502f7f7, $834fcccc,
$685c3434, $51f4a5a5, $d134e5e5, $f908f1f1, $e2937171, $ab73d8d8, $62533131, $2a3f1515,
$080c0404, $9552c7c7, $46652323, $9d5ec3c3, $30281818, $37a19696, $0a0f0505, $2fb59a9a,
$0e090707, $24361212, $1b9b8080, $df3de2e2, $cd26ebeb, $4e692727, $7fcdb2b2, $ea9f7575,
$121b0909, $1d9e8383, $58742c2c, $342e1a1a, $362d1b1b, $dcb26e6e, $b4ee5a5a, $5bfba0a0,
$a4f65252, $764d3b3b, $b761d6d6, $7dceb3b3, $527b2929, $dd3ee3e3, $5e712f2f, $13978484,
$a6f55353, $b968d1d1, $00000000, $c12ceded, $40602020, $e31ffcfc, $79c8b1b1, $b6ed5b5b,
$d4be6a6a, $8d46cbcb, $67d9bebe, $724b3939, $94de4a4a, $98d44c4c, $b0e85858, $854acfcf,
$bb6bd0d0, $c52aefef, $4fe5aaaa, $ed16fbfb, $86c54343, $9ad74d4d, $66553333, $11948585,
$8acf4545, $e910f9f9, $04060202, $fe817f7f, $a0f05050, $78443c3c, $25ba9f9f, $4be3a8a8,
$a2f35151, $5dfea3a3, $80c04040, $058a8f8f, $3fad9292, $21bc9d9d, $70483838, $f104f5f5,
$63dfbcbc, $77c1b6b6, $af75dada, $42632121, $20301010, $e51affff, $fd0ef3f3, $bf6dd2d2,
$814ccdcd, $18140c0c, $26351313, $c32fecec, $bee15f5f, $35a29797, $88cc4444, $2e391717,
$9357c4c4, $55f2a7a7, $fc827e7e, $7a473d3d, $c8ac6464, $bae75d5d, $322b1919, $e6957373,
$c0a06060, $19988181, $9ed14f4f, $a37fdcdc, $44662222, $547e2a2a, $3bab9090, $0b838888,
$8cca4646, $c729eeee, $6bd3b8b8, $283c1414, $a779dede, $bce25e5e, $161d0b0b, $ad76dbdb,
$db3be0e0, $64563232, $744e3a3a, $141e0a0a, $92db4949, $0c0a0606, $486c2424, $b8e45c5c,
$9f5dc2c2, $bd6ed3d3, $43efacac, $c4a66262, $39a89191, $31a49595, $d337e4e4, $f28b7979,
$d532e7e7, $8b43c8c8, $6e593737, $dab76d6d, $018c8d8d, $b164d5d5, $9cd24e4e, $49e0a9a9,
$d8b46c6c, $acfa5656, $f307f4f4, $cf25eaea, $caaf6565, $f48e7a7a, $47e9aeae, $10180808,
$6fd5baba, $f0887878, $4a6f2525, $5c722e2e, $38241c1c, $57f1a6a6, $73c7b4b4, $9751c6c6,
$cb23e8e8, $a17cdddd, $e89c7474, $3e211f1f, $96dd4b4b, $61dcbdbd, $0d868b8b, $0f858a8a,
$e0907070, $7c423e3e, $71c4b5b5, $ccaa6666, $90d84848, $06050303, $f701f6f6, $1c120e0e,
$c2a36161, $6a5f3535, $aef95757, $69d0b9b9, $17918686, $9958c1c1, $3a271d1d, $27b99e9e,
$d938e1e1, $eb13f8f8, $2bb39898, $22331111, $d2bb6969, $a970d9d9, $07898e8e, $33a79494,
$2db69b9b, $3c221e1e, $15928787, $c920e9e9, $8749cece, $aaff5555, $50782828, $a57adfdf,
$038f8c8c, $59f8a1a1, $09808989, $1a170d0d, $65dabfbf, $d731e6e6, $84c64242, $d0b86868,
$82c34141, $29b09999, $5a772d2d, $1e110f0f, $7bcbb0b0, $a8fc5454, $6dd6bbbb, $2c3a1616);
{$ifdef AES_LONGBOX}
Te4: array[byte] of longint =
($63636363, $7c7c7c7c, $77777777, $7b7b7b7b, $f2f2f2f2, $6b6b6b6b, $6f6f6f6f, $c5c5c5c5,
$30303030, $01010101, $67676767, $2b2b2b2b, $fefefefe, $d7d7d7d7, $abababab, $76767676,
$cacacaca, $82828282, $c9c9c9c9, $7d7d7d7d, $fafafafa, $59595959, $47474747, $f0f0f0f0,
$adadadad, $d4d4d4d4, $a2a2a2a2, $afafafaf, $9c9c9c9c, $a4a4a4a4, $72727272, $c0c0c0c0,
$b7b7b7b7, $fdfdfdfd, $93939393, $26262626, $36363636, $3f3f3f3f, $f7f7f7f7, $cccccccc,
$34343434, $a5a5a5a5, $e5e5e5e5, $f1f1f1f1, $71717171, $d8d8d8d8, $31313131, $15151515,
$04040404, $c7c7c7c7, $23232323, $c3c3c3c3, $18181818, $96969696, $05050505, $9a9a9a9a,
$07070707, $12121212, $80808080, $e2e2e2e2, $ebebebeb, $27272727, $b2b2b2b2, $75757575,
$09090909, $83838383, $2c2c2c2c, $1a1a1a1a, $1b1b1b1b, $6e6e6e6e, $5a5a5a5a, $a0a0a0a0,
$52525252, $3b3b3b3b, $d6d6d6d6, $b3b3b3b3, $29292929, $e3e3e3e3, $2f2f2f2f, $84848484,
$53535353, $d1d1d1d1, $00000000, $edededed, $20202020, $fcfcfcfc, $b1b1b1b1, $5b5b5b5b,
$6a6a6a6a, $cbcbcbcb, $bebebebe, $39393939, $4a4a4a4a, $4c4c4c4c, $58585858, $cfcfcfcf,
$d0d0d0d0, $efefefef, $aaaaaaaa, $fbfbfbfb, $43434343, $4d4d4d4d, $33333333, $85858585,
$45454545, $f9f9f9f9, $02020202, $7f7f7f7f, $50505050, $3c3c3c3c, $9f9f9f9f, $a8a8a8a8,
$51515151, $a3a3a3a3, $40404040, $8f8f8f8f, $92929292, $9d9d9d9d, $38383838, $f5f5f5f5,
$bcbcbcbc, $b6b6b6b6, $dadadada, $21212121, $10101010, $ffffffff, $f3f3f3f3, $d2d2d2d2,
$cdcdcdcd, $0c0c0c0c, $13131313, $ecececec, $5f5f5f5f, $97979797, $44444444, $17171717,
$c4c4c4c4, $a7a7a7a7, $7e7e7e7e, $3d3d3d3d, $64646464, $5d5d5d5d, $19191919, $73737373,
$60606060, $81818181, $4f4f4f4f, $dcdcdcdc, $22222222, $2a2a2a2a, $90909090, $88888888,
$46464646, $eeeeeeee, $b8b8b8b8, $14141414, $dededede, $5e5e5e5e, $0b0b0b0b, $dbdbdbdb,
$e0e0e0e0, $32323232, $3a3a3a3a, $0a0a0a0a, $49494949, $06060606, $24242424, $5c5c5c5c,
$c2c2c2c2, $d3d3d3d3, $acacacac, $62626262, $91919191, $95959595, $e4e4e4e4, $79797979,
$e7e7e7e7, $c8c8c8c8, $37373737, $6d6d6d6d, $8d8d8d8d, $d5d5d5d5, $4e4e4e4e, $a9a9a9a9,
$6c6c6c6c, $56565656, $f4f4f4f4, $eaeaeaea, $65656565, $7a7a7a7a, $aeaeaeae, $08080808,
$babababa, $78787878, $25252525, $2e2e2e2e, $1c1c1c1c, $a6a6a6a6, $b4b4b4b4, $c6c6c6c6,
$e8e8e8e8, $dddddddd, $74747474, $1f1f1f1f, $4b4b4b4b, $bdbdbdbd, $8b8b8b8b, $8a8a8a8a,
$70707070, $3e3e3e3e, $b5b5b5b5, $66666666, $48484848, $03030303, $f6f6f6f6, $0e0e0e0e,
$61616161, $35353535, $57575757, $b9b9b9b9, $86868686, $c1c1c1c1, $1d1d1d1d, $9e9e9e9e,
$e1e1e1e1, $f8f8f8f8, $98989898, $11111111, $69696969, $d9d9d9d9, $8e8e8e8e, $94949494,
$9b9b9b9b, $1e1e1e1e, $87878787, $e9e9e9e9, $cececece, $55555555, $28282828, $dfdfdfdf,
$8c8c8c8c, $a1a1a1a1, $89898989, $0d0d0d0d, $bfbfbfbf, $e6e6e6e6, $42424242, $68686868,
$41414141, $99999999, $2d2d2d2d, $0f0f0f0f, $b0b0b0b0, $54545454, $bbbbbbbb, $16161616);
{$endif}
{$ifdef StrictLong}
{$warnings on}
{$ifdef RangeChecks_on}
{$R+}
{$endif}
{$endif}
{$ifdef AES_LONGBOX}
const
X000000ff = longint($000000ff); {Avoid D4+ warnings}
X0000ff00 = longint($0000ff00);
X00ff0000 = longint($00ff0000);
Xff000000 = longint($ff000000);
{$endif}

View File

@ -0,0 +1,72 @@
(*************************************************************************
Include file for AES_ENCR.PAS - AES_Encrypt for Pascal16/Full tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version from AES_ENCR.PAS
0.11 16.11.08 we Use Ptr2Inc from BTypes
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{Normally used for TP5/5.5 (and during development BP7)}
{---------------------------------------------------------------------------}
procedure AES_Encrypt(var ctx: TAESContext; {$ifdef CONST} const {$else} var {$endif} BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
label done;
var
pK: PWA4; {pointer to loop rount key}
r: integer;
t,s: TAESBlock;
begin
{Setup key pointer}
pK := PWA4(@ctx.RK);
{Initialize with input block}
TWA4(s)[0] := TWA4(BI)[0] xor pK^[0];
TWA4(s)[1] := TWA4(BI)[1] xor pK^[1];
TWA4(s)[2] := TWA4(BI)[2] xor pK^[2];
TWA4(s)[3] := TWA4(BI)[3] xor pK^[3];
inc(Ptr2Inc(pK), 4*sizeof(longint));
r := 1;
while true do begin
TWA4(t)[0] := Te0[s[0*4+0]] xor Te1[s[1*4+1]] xor Te2[s[2*4+2]] xor Te3[s[3*4+3]] xor pK^[0];
TWA4(t)[1] := Te0[s[1*4+0]] xor Te1[s[2*4+1]] xor Te2[s[3*4+2]] xor Te3[s[0*4+3]] xor pK^[1];
TWA4(t)[2] := Te0[s[2*4+0]] xor Te1[s[3*4+1]] xor Te2[s[0*4+2]] xor Te3[s[1*4+3]] xor pK^[2];
TWA4(t)[3] := Te0[s[3*4+0]] xor Te1[s[0*4+1]] xor Te2[s[1*4+2]] xor Te3[s[2*4+3]] xor pK^[3];
inc(Ptr2Inc(pK), 4*sizeof(longint));
inc(r);
if r>=ctx.rounds then goto done;
TWA4(s)[0] := Te0[t[0*4+0]] xor Te1[t[1*4+1]] xor Te2[t[2*4+2]] xor Te3[t[3*4+3]] xor pK^[0];
TWA4(s)[1] := Te0[t[1*4+0]] xor Te1[t[2*4+1]] xor Te2[t[3*4+2]] xor Te3[t[0*4+3]] xor pK^[1];
TWA4(s)[2] := Te0[t[2*4+0]] xor Te1[t[3*4+1]] xor Te2[t[0*4+2]] xor Te3[t[1*4+3]] xor pK^[2];
TWA4(s)[3] := Te0[t[3*4+0]] xor Te1[t[0*4+1]] xor Te2[t[1*4+2]] xor Te3[t[2*4+3]] xor pK^[3];
inc(Ptr2Inc(pK), 4*sizeof(longint));
inc(r);
end;
done:
s[00] := SBox[t[0*4+0]];
s[01] := SBox[t[1*4+1]];
s[02] := SBox[t[2*4+2]];
s[03] := SBox[t[3*4+3]];
s[04] := SBox[t[1*4+0]];
s[05] := SBox[t[2*4+1]];
s[06] := SBox[t[3*4+2]];
s[07] := SBox[t[0*4+3]];
s[08] := SBox[t[2*4+0]];
s[09] := SBox[t[3*4+1]];
s[10] := SBox[t[0*4+2]];
s[11] := SBox[t[1*4+3]];
s[12] := SBox[t[3*4+0]];
s[13] := SBox[t[0*4+1]];
s[14] := SBox[t[1*4+2]];
s[15] := SBox[t[2*4+3]];
TWA4(BO)[0] := TWA4(s)[0] xor pK^[0];
TWA4(BO)[1] := TWA4(s)[1] xor pK^[1];
TWA4(BO)[2] := TWA4(s)[2] xor pK^[2];
TWA4(BO)[3] := TWA4(s)[3] xor pK^[3];
end;

View File

@ -0,0 +1,88 @@
(*************************************************************************
Include file for AES_ENCR.PAS - AES_Encrypt for BIT32/Full tables
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 09.07.06 W.Ehrhardt Initial version from AES_ENCR.PAS
**************************************************************************)
(**** (C) Copyright 2002-2006 Wolfgang Ehrhardt -- see copying_we.txt ****)
{ 32 Bit code: Alternative versions can be found in options.zip
enc_full.inc - fully unrolled version for highest speed
enc_ptr.inc - pointer version (may be faster on some systems)
}
{---------------------------------------------------------------------------}
procedure AES_Encrypt(var ctx: TAESContext; const BI: TAESBlock; var BO: TAESBlock);
{-encrypt one block, not checked: key must be encryption key}
var
r: integer; {round loop countdown counter}
pK: PWA4; {pointer to loop rount key }
s3,s0,s1,s2: longint; {TAESBlock s as separate variables}
t: TWA4;
begin
{Setup key pointer}
pK := PWA4(@ctx.RK);
{Initialize with input block}
s0 := TWA4(BI)[0] xor pK^[0];
s1 := TWA4(BI)[1] xor pK^[1];
s2 := TWA4(BI)[2] xor pK^[2];
s3 := TWA4(BI)[3] xor pK^[3];
inc(pK);
{perform encryption rounds}
for r:=1 to ctx.Rounds-1 do begin
t[0] := Te0[s0 and $ff] xor Te1[s1 shr 8 and $ff] xor Te2[s2 shr 16 and $ff] xor Te3[s3 shr 24] xor pK^[0];
t[1] := Te0[s1 and $ff] xor Te1[s2 shr 8 and $ff] xor Te2[s3 shr 16 and $ff] xor Te3[s0 shr 24] xor pK^[1];
t[2] := Te0[s2 and $ff] xor Te1[s3 shr 8 and $ff] xor Te2[s0 shr 16 and $ff] xor Te3[s1 shr 24] xor pK^[2];
s3 := Te0[s3 and $ff] xor Te1[s0 shr 8 and $ff] xor Te2[s1 shr 16 and $ff] xor Te3[s2 shr 24] xor pK^[3];
s0 := t[0];
s1 := t[1];
s2 := t[2];
inc(pK);
end;
{$ifdef AES_LONGBOX}
{Use expanded longint SBox table Te4 from [2]}
TWA4(BO)[0] := (Te4[s0 and $ff] and X000000ff) xor
(Te4[s1 shr 8 and $ff] and X0000ff00) xor
(Te4[s2 shr 16 and $ff] and X00ff0000) xor
(Te4[s3 shr 24 and $ff] and Xff000000) xor pK^[0];
TWA4(BO)[1] := (Te4[s1 and $ff] and X000000ff) xor
(Te4[s2 shr 8 and $ff] and X0000ff00) xor
(Te4[s3 shr 16 and $ff] and X00ff0000) xor
(Te4[s0 shr 24 and $ff] and Xff000000) xor pK^[1];
TWA4(BO)[2] := (Te4[s2 and $ff] and X000000ff) xor
(Te4[s3 shr 8 and $ff] and X0000ff00) xor
(Te4[s0 shr 16 and $ff] and X00ff0000) xor
(Te4[s1 shr 24 and $ff] and Xff000000) xor pK^[2];
TWA4(BO)[3] := (Te4[s3 and $ff] and X000000ff) xor
(Te4[s0 shr 8 and $ff] and X0000ff00) xor
(Te4[s1 shr 16 and $ff] and X00ff0000) xor
(Te4[s2 shr 24 and $ff] and Xff000000) xor pK^[3];
{$else}
{Uses Sbox and shl, needs type cast longint() for}
{16 bit compilers: here Sbox is byte, Te4 is longint}
TWA4(BO)[0] := (longint(SBox[s0 and $ff]) xor
longint(SBox[s1 shr 8 and $ff]) shl 8 xor
longint(SBox[s2 shr 16 and $ff]) shl 16 xor
longint(SBox[s3 shr 24]) shl 24 ) xor pK^[0];
TWA4(BO)[1] := (longint(SBox[s1 and $ff]) xor
longint(SBox[s2 shr 8 and $ff]) shl 8 xor
longint(SBox[s3 shr 16 and $ff]) shl 16 xor
longint(SBox[s0 shr 24]) shl 24 ) xor pK^[1];
TWA4(BO)[2] := (longint(SBox[s2 and $ff]) xor
longint(SBox[s3 shr 8 and $ff]) shl 8 xor
longint(SBox[s0 shr 16 and $ff]) shl 16 xor
longint(SBox[s1 shr 24]) shl 24 ) xor pK^[2];
TWA4(BO)[3] := (longint(SBox[s3 and $ff]) xor
longint(SBox[s0 shr 8 and $ff]) shl 8 xor
longint(SBox[s1 shr 16 and $ff]) shl 16 xor
longint(SBox[s2 shr 24]) shl 24 ) xor pK^[3];
{$endif}
end;

View File

@ -0,0 +1,34 @@
---------------------------------------------------------------------------
Legal Notice
Some of my software/programs contain cryptographic algorithms. There are
countries that restrict the use, import, export of cryptographic software.
Before keeping, using, or distributing the software, make sure that you
comply to these restrictions. If (for any reason) you are unable to do so,
you are not allowed to download, use, distribute the software.
If you are residing in a country that allows software patents you must
verify that no part of the software is covered by a patent in your country.
If (for any reason) you are unable to do so, you are not allowed to use or
distribute the software.
---------------------------------------------------------------------------
Rechtlicher Hinweis
Einige meiner Software/Programme enthalten kryptographische Algorithmen. Es
gibt Laender, die den Gebrauch, Import, Export von kryptographischer Software
einschraenken bzw. verbieten. Vor Besitz, Gebrauch, Verbreitung dieser
Software/Programme in diese(n) Laendern muss sichergestellt sein, dass
diesen Beschraenkungen entsprochen wird. Sollte das (aus welchen Gruenden
auch immer) nicht moeglich sein, darf die Software nicht heruntergeladen,
benutzt oder verbreitet werden.
Einige Laender erlauben Softwarepatente. Benutzer aus solchen Laendern
muessen sicherstellen, dass die Software (oder Teile davon) keine Patente
beruehrt oder verletzt. Sollte das (aus welchen Gruenden auch immer) nicht
moeglich sein, darf die Software nicht benutzt oder verbreitet werden.
---------------------------------------------------------------------------
http://wolfgang-ehrhardt.de

View File

@ -0,0 +1,383 @@
unit Mem_Util;
{Utility procedures for Hex/Base64 and memory compare}
interface
{$i STD.INC}
(*************************************************************************
DESCRIPTION : Utility procedures for Hex/Base64 and memory compare
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12, FPC, VP, WDOSX
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
REFERENCES : RFC 3548 - The Base16, Base32, and Base64 Data Encodings
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.10 01.01.02 W.Ehrhardt Initial version
0.20 30.08.03 we with pointer valid for all compilers
0.30 17.09.03 we with HexLong
0.40 27.09.03 we FPC/go32v2
0.50 05.10.03 we STD.INC
0.60 10.10.03 we english comments
0.70 26.12.03 we Base64Str
0.80 12.04.04 we HexUpper, Delphi 7
0.81 12.06.04 we handle nil pointers
0.90 05.12.04 we Hex2Mem
0.91 31.10.05 we Simple Base64Enc/DecStr, D9/WDOSX, Base64Str with result
0.92 11.12.05 we Bugfix: Hex2Mem and $R+
0.93 07.02.06 we RandMem
0.94 14.10.07 we HexWord
0.95 25.09.08 we uses BTypes
0.96 14.11.08 we BString, char8, Ptr2Inc
0.97 05.07.09 we D12 fix for Hex2Mem
0.98 27.07.10 we CompMemXL, RandMemXL
0.99 25.09.10 we CompMemXL returns true if size <= 0
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2002-2010 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
uses
BTypes;
var
HexUpper: boolean; {Hex strings in uppercase}
function HexByte(b: byte): BString;
{-byte as hex string}
function HexWord(w: word): BString;
{-word as hex string}
function HexLong(L: longint): BString;
{-longint as hex string, LSB first}
function HexStr(psrc: pointer; L: integer): BString;
{-hex string of memory block of length L pointed by psrc}
procedure Hex2Mem({$ifdef CONST}const{$endif} s: BString; pdest: pointer; MaxL: word; var L: word);
{-Convert hex string to mem pointed by pdest, MaxL bytes, actual byte count in L}
function Base64Str(psrc: pointer; L: integer): BString;
{-Base64 string of memory block of length L pointed by psrc}
function Base64EncStr({$ifdef CONST}const{$endif} s: BString): BString;
{-Simple Base64 encoder, uses Base64Str}
function Base64DecStr({$ifdef CONST}const{$endif} es: BString): BString;
{-Simple Base64 decoder, stops conversion on first invalid char}
function CompMem(psrc, pdest: pointer; size: word): boolean;
{-compare memory block}
procedure RandMem(pdest: pointer; size: word);
{-fill memory block with size random bytes}
function CompMemXL(psrc, pdest: pointer; size: longint): boolean;
{-compare memory block}
procedure RandMemXL(pdest: pointer; size: longint);
{-fill memory block with size random bytes}
implementation
const
CT64: array[0..63] of char8 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
{---------------------------------------------------------------------------}
function HexByte(b: byte): BString;
{-byte as hex string}
const
nib: array[0..15] of char8 = '0123456789abcdef';
begin
if HexUpper then HexByte := upcase(nib[b div 16]) + upcase(nib[b and 15])
else HexByte := nib[b div 16] + nib[b and 15];
end;
{---------------------------------------------------------------------------}
function HexWord(w: word): BString;
{-word as hex string}
begin
HexWord := HexByte(w shr 8)+HexByte(w and $FF);
end;
{---------------------------------------------------------------------------}
function HexLong(L: longint): BString;
{-longint as hex string, LSB first}
var
i: integer;
s: string[8];
begin
s := '';
for i:=0 to 3 do begin
s := HexByte(L and $FF) + s;
L := L shr 8;
end;
HexLong := s;
end;
{---------------------------------------------------------------------------}
function HexStr(psrc: pointer; L: integer): BString;
{-hex string of memory block of length L pointed by psrc}
var
i: integer;
s: BString;
begin
s := '';
if psrc<>nil then begin
for i:=0 to L-1 do begin
s := s + HexByte(pByte(psrc)^);
inc(Ptr2Inc(psrc));
end;
end;
HexStr := s;
end;
{---------------------------------------------------------------------------}
procedure Hex2Mem({$ifdef CONST}const{$endif} s: BString; pdest: pointer; MaxL: word; var L: word);
{-Convert hex string to mem pointed by pdest, MaxL bytes, actual byte count in L}
const
nib: array[0..15] of char8 = '0123456789ABCDEF';
wsp: array[0..3] of char8 = #32#9#13#10;
label
_break; {for versions without break}
var
i,p: integer;
b: byte;
c: char8;
bdone: boolean; {flag byte complete}
begin
L := 0;
if MaxL=0 then exit;
bdone := true;
b := 0;
for i:=1 to length(s) do begin
c := upcase(s[i]);
p := pos(c,nib)-1;
if p>=0 then begin
{Insert new nibble into b. If range checking is on, we}
{must prevent the following shift from overflowing b. }
{$ifopt R+}
b := ((b and $F) shl 4) or (p and $0F);
{$else}
b := (b shl 4) or (p and $0F);
{$endif}
bdone := not bdone;
if bdone then begin
{byte complete, store or break}
if L<MaxL then begin
pByte(pdest)^ := b;
inc(Ptr2Inc(pdest));
inc(L);
end
else goto _break;
end;
end
else begin
{ignore white space}
if pos(c,wsp)=0 then goto _break;
end;
end;
_break:
if (not bdone) and (L<MaxL) then begin
{store remaining nibble}
pByte(pdest)^ := (b and $0F) shl 4;
inc(L);
end;
end;
{---------------------------------------------------------------------------}
function Base64Str(psrc: pointer; L: integer): BString;
{-Base64 string of memory block of length L pointed by psrc}
var
q,r: integer;
b0,b1,b2: byte;
{$ifndef RESULT}
result: BString;
{$endif}
begin
result := '';
if (L>0) and (psrc<>nil) then begin
q := L div 3;
r := L mod 3;
while q>0 do begin
b0 := pByte(psrc)^; inc(Ptr2Inc(psrc));
b1 := pByte(psrc)^; inc(Ptr2Inc(psrc));
b2 := pByte(psrc)^; inc(Ptr2Inc(psrc));
result := result + CT64[(b0 shr 2) and $3f]
+ CT64[((b0 shl 4) and $30) or ((b1 shr 4) and $0f)]
+ CT64[((b1 shl 2) and $3c) or ((b2 shr 6) and $03)]
+ CT64[b2 and $3f];
dec(q);
end;
if r=2 then begin
b0 := pByte(psrc)^; inc(Ptr2Inc(psrc));
b1 := pByte(psrc)^;
result := result + CT64[(b0 shr 2) and $3f]
+ CT64[((b0 shl 4) and $30) or ((b1 shr 4) and $0f)]
+ CT64[(b1 shl 2) and $3c]
+ '=';
end
else if r=1 then begin
b0 := pByte(psrc)^;
result := result + CT64[(b0 shr 2) and $3f]
+ CT64[(b0 shl 4) and $30]
+ '==';
end;
end;
{$ifndef RESULT}
Base64Str := result;
{$endif}
end;
{---------------------------------------------------------------------------}
function Base64EncStr({$ifdef CONST}const{$endif} s: BString): BString;
{-Simple Base64 encoder, uses Base64Str}
begin
Base64EncStr := Base64Str(@s[1], length(s));
end;
{---------------------------------------------------------------------------}
function Base64DecStr({$ifdef CONST}const{$endif} es: BString): BString;
{-Simple Base64 decoder, stops conversion on first invalid char}
var
i,bits,buf: word;
{$ifndef RESULT}
result: BString;
{$endif}
ic: array[char8] of byte;
b: byte;
label
_break; {for TP5/5.5}
begin
{Note: this is a stripped down version of Base2N.Decode2NPrim}
result := '';
{Fill input array with Base64 digit values, $FF if not valid}
fillchar(IC, sizeof(IC), $FF);
for i:=0 to 63 do ic[CT64[i]] := i;
buf := 0;
bits := 0;
for i:=1 to length(es) do begin
b := IC[es[i]];
if b>127 then goto _break;
{Include next input into buffer. If range checking is on, }
{we must prevent the following shift from overflowing buf.}
{$ifopt R+}
buf := ((buf and $03FF) shl 6) or b;
{$else}
buf := (buf shl 6) or b;
{$endif}
inc(bits,6);
if bits>7 then begin
{output a byte if at least 8 bits in input buf}
dec(bits,8);
result := result + char8((buf shr bits) and $FF);
end;
end;
_break:
{$ifndef RESULT}
Base64DecStr := result;
{$endif}
end;
{---------------------------------------------------------------------------}
function CompMemXL(psrc, pdest: pointer; size: longint): boolean;
{-compare memory block}
var
i: longint;
begin
if size>0 then begin
CompMemXL := false;
if (psrc=nil) or (pdest=nil) then exit;
for i:=1 to size do begin
if pByte(psrc)^<>pByte(pdest)^ then exit;
inc(Ptr2Inc(psrc));
inc(Ptr2Inc(pdest));
end;
end;
CompMemXL := true;
end;
{---------------------------------------------------------------------------}
procedure RandMemXL(pdest: pointer; size: longint);
{-fill memory block with size random bytes}
var
i: longint;
begin
if pdest<>nil then begin
for i:=1 to size do begin
pByte(pdest)^ := random(256);
inc(Ptr2Inc(pdest));
end;
end;
end;
{---------------------------------------------------------------------------}
function CompMem(psrc, pdest: pointer; size: word): boolean;
{-compare memory block}
begin
CompMem := CompMemXL(psrc, pdest, size);
end;
{---------------------------------------------------------------------------}
procedure RandMem(pdest: pointer; size: word);
{-fill memory block with size random bytes}
begin
RandMemXL(pdest, size);
end;
begin
HexUpper := false;
end.

View File

@ -0,0 +1,108 @@
This archive contains AES (Advanced Encryption Standard) related Pascal /
Delphi sources: basic AES routines and recommended block cipher modes of
operation (with test programs that verify compilation and results).
The block level routines supply separate units for encryption and decryption.
The source code for basic encryption/decryption is split into several include
files. At the lowest level there are type definitions and common routines. Key
sizes of 128, 192, and 256 bits are supported.
The following recommended block cipher modes of operation are implemented:
CBC, CFB128, CFB8, CTR, ECB, OFB, OMAC, CMAC, CCM, EAX, GCM, and XTS. All
chaining modes allow plain and cipher text lengths that need not be multiples
of the block length (for ECB and CBC cipher text stealing is used for the
short block; only one short block is allowed and there must be at least one
full block). CTR mode can use 4 built-in incrementing functions or a user
supplied one, and provides seek functions for random access reads.
All routines have been included in the AES_DLL.DLL, there are two interface
units for this DLL (one for Virtual Pascal, the second for the other Win32
compilers).
Since the July 2006 release there are conditional defines to support
compressed tables: one 2K encryption table (calculated with t_mkctab) replaces
the four 1K tables (same for decryption, here the inverse SBox is no longer
needed). Besides using less static memory, compressed tables are considered as
a countermeasure against cache timing attacks.
W.Ehrhardt, Nov. 2017
http://wolfgang-ehrhardt.de
-------------------------------------------------------------------------------
Last changes:
Nov. 2017
- FPC/ARM and Delphi Tokyo adjustments
Sep. 2015
- Constant time verification/compare for the all-in-one packet
functions (aes_eax, aes_gcm, aes_ccm)
Jan. 2013
- Adjustments (test programs) for D17 (XE3), {$J+} if needed
Dec. 2012
- Small 64-bit adjustments (separate BIT64 include statements in
aes_decr and aes_encr; improved aes_gcm)
July 2012
- 64-bit adjustment for GCM
Oct. 2010
- Galois/Counter Mode (GCM)
- Fix PPP unit for TP5
Aug. 2010
- Message length ILen has now type longint
- New PPP unit (Perfect Paper Passwords)
June 2010
- AES_CTR_Seek functions
July 2009
- Delphi 2009 (D12) adjustments
May 2009
- Counter with CBC-MAC (CCM) mode
Nov. 2008
- Uses the BTypes unit for better portability
Aug. 2008
- All-in-one EAX functions for encrypt / authenticate and decrypt / verify:
decryption is performed only if the verification was successful.
- Range check safe IncProcs for FPC -dDebug
Jan. 2008
New unit aes_cfb8 implementing the 8 bit CFB mode
Oct. 2007
- New unit aes_xts implementing the XTS mode from the IEEE P1619 Draft Standard
for Cryptographic Protection of Data on Block-Oriented Storage Devices.
June 2007
- AES-CMAC-PRF-128 from RFC 4615
- New EAX context name
Nov. 2006
- Contributed AES256 file crypt/authenticate unit
July 2006
- CMAC mode, compressed tables as a countermeasure against cache timing attacks
Jul. 2004
- EAX mode, AES DLL, new demo programs
Jun. 2004
- OMAC mode on AES page
Mar. 2004
- Significant speedup of AES key generation
Jan. 2004
- New faster AES routines
Dec. 2003
- First version of AES archive released

View File

@ -0,0 +1,631 @@
(*************************************************************************
DESCRIPTION : Standard definitions and options
REQUIREMENTS : TP5-7, D1-D7/D9-D12/D14-D25, FPC, VP, (TPW1.0/1.5,BCB3/4)
Version Date Author Modification
------- -------- ------- ------------------------------------------
1.00 05.10.03 W.Ehrhardt Initial version
1.01 05.10.03 we X_OPT, removed TP4
1.02 30.10.03 we WINCRT
1.03 09.12.03 we {$R+,S+} {$ifdef debug}
1.04 26.12.03 we VP: {&Optimise+,SmartLink+,Speed+} ifndef debug
1.05 28.12.03 we DELPHI = Delphi32 (no Delphi 1!)
1.06 12.04.04 we Delphi 7
1.07 26.09.04 we Record starting values of important options
1.08 10.10.04 we RESULT for Result pseudo variable
1.09 02.01.05 we BIT16: default $F-
1.10 26.02.05 we StrictLong
1.11 05.05.05 we D9 aka Delphi 2005
1.12 22.05.05 we StrictLong for FPC 2.0
1.13 27.05.05 we {$goto on} for FPC
1.14 27.05.05 we moved {$goto on} to default settings
1.15 29.05.05 we HAS_INT64, HAS_MSG, _STD_INC_
1.16 06.08.05 we J_OPT, N_OPT, HAS_INLINE
1.17 17.08.05 we HAS_ASSERT
1.18 08.11.05 we APPCONS, partial TMT,TPW15 support
1.19 20.11.05 we Default option {$B-}
1.20 08.01.06 we ABSTRACT/DEFAULT
1.21 08.02.06 we Fix Scanhelp quirk
1.22 11.02.06 we VER5X
1.23 15.04.06 we HAS_XTYPES
1.24 08.05.06 we D10 aka Delphi 2006
1.25 25.05.06 we Define RESULT if FPC_OBJFPC is defined
1.26 08.09.06 we Define RESULT/DEFAULT if FPC_DELPHI is defined
1.27 14.11.06 we HAS_ASSERT for FPC VER1 and VER2
1.28 28.11.06 we HAS_UNSAFE, $warn SYMBOL_../UNSAFE_.. OFF
1.29 25.05.07 we D11 aka Delphi 2007, FPC2.1.4
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
1.36 07.09.08 we HAS_CARD32
1.37 21.11.08 we D12 aka D2009
1.38 19.02.09 we TPW 1.0 adjustments
1.39 05.07.09 we D12Plus
1.40 17.10.09 we BASM (BASM16 or Bit32)
1.41 21.10.09 we HAS_OVERLOAD
1.42 07.04.10 we HAS_DENORM_LIT (Denormalised extended literals, e.g. -1.23e-4942)
1.43 20.06.10 we D14 (VER210)
1.45 16.10.10 we WIN16
1.46 05.11.10 we FPC VER2_4
1.47 12.11.11 we FPC VER2_6
1.48 01.01.12 we HAS_UINT64 for FPC VER2_6
1.49 12.01.12 we BIT64, WIN32or64, Bit32or64
1.50 13.01.12 we EXT64 (64 bit extended = double)
1.51 19.01.12 we Define EXT64 if SIMULATE_EXT64
1.52 05.09.12 we Basic support for D14, D15(XE), D16(XE2), D17(XE3)
1.53 01.12.12 we Simplified FPC 2.X.Y definitions
1.54 17.12.12 we UNIT_SCOPE (D16/D17)
1.55 25.12.12 we J_OPT for BIT64
1.56 25.04.13 we D18/XE4 (VER250)
1.57 28.09.13 we Basic support for D19/XE5 (VER260)
1.58 17.04.14 we Basic support for D20/XE6 (VER270)
1.59 06.05.14 we FPC/CPUARM: $define EXT64, i.e. no FP 80-bit extended
1.60 13.09.14 we Basic support for D21/XE7 (VER280)
1.61 22.10.14 we HAS_OUT
1.62 13.01.15 we FPC VER3 (FPC3.0.1/3.1.1), FPC2Plus, FPC271or3
1.63 22.04.15 we Basic support for D22/XE8 (VER290)
1.64 25.04.15 we HAS_INTXX, HAS_PINTXX
1.65 01.09.15 we Basic support for D23 (VER300) 'Seattle'
1.66 26.04.16 we Basic support for D24 (VER310) 'Berlin'
1.67 17.03.17 we Define PurePascal for FPC/CPUARM
1.68 11.04.17 we Basic support for D25 (VER320) 'Tokyo'
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2002-2017 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
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{$ifndef _STD_INC_}
{$define _STD_INC_} {include STD.INC only once}
{.$undef BIT16} {16 Bit code, Pascal / D1}
{.$undef BIT32} {32 Bit code}
{.$undef BIT64} {64 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 WIN16} {Compiler for 16-bit windows}
{.$undef WIN32or64} {Compiler for 32/64-bit windows}
{.$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_CARD32} {Has 32 bit cardinal}
{.$undef HAS_MSG} {Has message directive}
{.$undef HAS_INLINE} {Has inline procs/funcs (D9)}
{.$undef HAS_OUT} {Has OUT parameters: D3+, FPC2+ Delphi/ObjFPC}
{.$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 FPC2Plus} {FPC 2 or newer}
{.$undef FPC271or3} {FPC 271 or 3 (less accurate for 64 bit or SSE2)}
{.$undef D12PLUS} {Delphi 12 or higher}
{.$undef HAS_OVERLOAD} {Overloading of procedures and functions}
{.$undef HAS_DENORM_LIT} {Denormalised (extended) literals, e.g. -1.23e-4942}
{.$undef EXT64} {64 bit extended = double}
{.$undef UNIT_SCOPE} {Unit scope name, D16+}
{.$undef HAS_INTXX} {Int8 .. Int32, UInt8 .. UInt32}
{.$undef HAS_PINTXX} {pInt8 .. pInt32, pUInt8 .. pUInt32}
{$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 BASM} {BASM16 or BIT32}
{$define V7PLUS} {TP7 or higher}
{$ifdef VER10} {TPW 1.0}
{$define BIT16}
{$define BASM16}
{$define WINCRT}
{$define G_OPT}
{$undef CONST}
{$undef Q_OPT}
{$undef V7PLUS}
{$endif}
{$ifdef VER15} {TPW 1.5}
{$define BIT16}
{$define BASM16}
{$define WINCRT}
{$define G_OPT}
{$undef CONST}
{$undef Q_OPT}
{$undef V7PLUS}
{$endif}
{$ifdef VER50 }
{$define BIT16}
{$define VER5X}
{$undef BASM}
{$undef CONST}
{$undef Q_OPT}
{$undef X_OPT}
{$undef V7PLUS}
{$endif}
{$ifdef VER55 }
{$define BIT16}
{$define VER5X}
{$undef BASM}
{$undef CONST}
{$undef Q_OPT}
{$undef X_OPT}
{$undef V7PLUS}
{$endif}
{$ifdef VER60 }
{$define BIT16}
{$undef CONST}
{$undef Q_OPT}
{$define G_OPT}
{$define BASM16}
{$undef V7PLUS}
{$endif}
{$ifdef VER70 }
{$define BIT16}
{$define G_OPT}
{$define BASM16}
{$endif}
{$ifdef VER80}
{.$define DELPHI} {D1} {*we V1.05}
{$define BIT16 }
{$define G_OPT }
{$define BASM16}
{$define WINCRT}
{$define RESULT}
{$endif}
{$ifdef VER90 }
{$define DELPHI} {D2}
{$endif}
{$ifdef VER93 }
{$define DELPHI} {BCB++1}
{$endif}
{$ifdef VER100}
{$define DELPHI} {D3}
{$define HAS_ASSERT}
{$define HAS_OUT}
{$endif}
{$ifdef VER110}
{$define DELPHI} {BCB3}
{$define HAS_OUT}
{$endif}
{$ifdef VER120}
{$define DELPHI} {D4}
{$define D4PLUS}
{$endif}
{$ifdef VER125}
{$define DELPHI} {BCB4}
{$define D4PLUS}
{$endif}
{$ifdef VER130}
{$define DELPHI} {D5}
{$define D4PLUS}
{$endif}
{$ifdef VER140}
{$define DELPHI} {D6}
{$define D4PLUS}
{$endif}
{$ifdef VER150}
{$define DELPHI} {D7}
{$define D4PLUS}
{$define HAS_UNSAFE}
{$define HAS_UINT64}
{$endif}
{$ifdef VER170}
{$define DELPHI} {D9}
{$define D4PLUS}
{$define HAS_INLINE}
{$define HAS_UNSAFE}
{$define HAS_UINT64}
{$endif}
{$ifdef VER180}
{$define DELPHI} {D10, D11 ifdef VER185}
{$define D4PLUS}
{$define HAS_INLINE}
{$define HAS_UNSAFE}
{$define HAS_UINT64}
{$endif}
{$ifdef VER200}
{$define DELPHI} {D12}
{$define D12PLUS}
{$endif}
{$ifdef VER210}
{$define DELPHI} {D14}
{$define D12PLUS}
{$endif}
{$ifdef VER220}
{$define DELPHI} {D15 - XE}
{$define D12PLUS}
{$endif}
{$ifdef VER230}
{$define DELPHI} {D16 - XE2}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef VER240}
{$define DELPHI} {D17 - XE3}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef VER250}
{$define DELPHI} {D18 - XE4}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef VER260}
{$define DELPHI} {D19 - XE5}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef VER270}
{$define DELPHI} {D20 - XE6}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef VER280}
{$define DELPHI} {D21 - XE7}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef VER290}
{$define DELPHI} {D22 - XE8}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef VER300}
{$define DELPHI} {D23}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef VER310}
{$define DELPHI} {D24}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef VER320}
{$define DELPHI} {D25}
{$define D12PLUS}
{$define UNIT_SCOPE}
{$endif}
{$ifdef CONDITIONALEXPRESSIONS} {D6+}
{$ifndef D4PLUS}
{$define D4PLUS}
{$endif}
{$define HAS_MSG}
{$define HAS_XTYPES}
{$ifdef CPUX64}
{$define BIT64}
{$endif}
{$endif}
{$ifdef VER70}
{$ifdef windows}
{$define WINCRT}
{$endif}
{$endif}
{$ifdef VirtualPascal}
{$define G_OPT}
{$define RESULT}
{$define LoadArgs}
{$endif}
{$ifdef WIN32}
{$define J_OPT}
{$endif}
{$ifdef BIT64}
{$define J_OPT}
{$endif}
{$ifdef FPC}
{$define FPC_ProcVar}
{$define ABSTRACT}
{$define HAS_XTYPES}
{$define HAS_OVERLOAD}
{$undef N_OPT}
{$ifdef VER1}
{$undef J_OPT}
{$define HAS_INT64}
{$define HAS_CARD32}
{$define HAS_MSG}
{$define HAS_ASSERT}
{$ifndef VER1_0}
{FPC 1.9.x}
{$define StrictLong}
{$else}
{$define LoadArgs}
{$endif}
{$endif}
{$ifdef VER2}
{$define FPC2Plus}
{$define HAS_ASSERT}
{$define HAS_INT64}
{$define HAS_CARD32}
{$define HAS_MSG}
{$define HAS_INLINE} {Remember to use -Si}
{$define StrictLong}
{$ifdef FPC_OBJFPC}
{$define DEFAULT}
{$endif}
{$ifdef FPC_DELPHI}
{$define DEFAULT}
{$endif}
{$ifndef VER2_0}
{$ifndef VER2_1}
{$define HAS_UINT64} {2.2+}
{$endif}
{$define HAS_DENORM_LIT} {2.1+}
{$endif}
{$ifdef VER2_7_1}
{$define FPC271or3}
{$endif}
{$ifdef VER2_6_2}
{$define HAS_INTXX}
{$endif}
{$ifdef VER2_6_4}
{$define HAS_INTXX}
{$define HAS_PINTXX}
{$endif}
{$endif}
{$ifdef VER3}
{$define FPC2Plus}
{$define FPC271or3}
{$define HAS_ASSERT}
{$define HAS_INT64}
{$define HAS_CARD32}
{$define HAS_MSG}
{$define HAS_INLINE}
{$define HAS_UINT64}
{$define HAS_DENORM_LIT}
{$define StrictLong}
{$define HAS_INTXX}
{$define HAS_PINTXX}
{$ifdef FPC_OBJFPC}
{$define DEFAULT}
{$endif}
{$ifdef FPC_DELPHI}
{$define DEFAULT}
{$endif}
{$endif}
{Note: Mode detection does not work for -Sxxx and version < 2.0.2}
{$ifdef FPC_OBJFPC}
{$define RESULT}
{$define HAS_OUT}
{$endif}
{$ifdef FPC_DELPHI}
{$define RESULT}
{$define HAS_OUT}
{$undef FPC_ProcVar}
{$endif}
{$ifdef FPC_TP}
{$undef FPC_ProcVar}
{$endif}
{$ifdef FPC_GPC}
{$undef FPC_ProcVar}
{$endif}
{$ifdef CPU64}
{$define BIT64}
{$endif}
{$ifdef CPUARM}
{$define EXT64} {No extended for ARM}
{$define PurePascal}
{$endif}
{$endif}
{$ifdef __TMT__}
{$undef N_OPT}
{$define RESULT}
{$define HAS_INT64}
{$define LoadArgs}
{$ifdef __WIN32__}
{$define WIN32}
{$endif}
{$endif}
{$ifndef BIT16}
{$define Bit32or64}
{$ifndef BIT64}
{$define BIT32}
{$endif}
{$endif}
{$ifdef BIT16}
{$ifdef WINDOWS}
{$define WIN16}
{$endif}
{$endif}
{$ifdef Delphi}
{$define RESULT}
{$define ABSTRACT}
{$define HAS_DENORM_LIT}
{$endif}
{$ifdef D12Plus}
{$ifndef D4PLUS}
{$define D4PLUS}
{$endif}
{$define HAS_INLINE}
{$define HAS_UNSAFE}
{$define HAS_UINT64}
{$define HAS_INTXX}
{$endif}
{$ifdef D4Plus}
{$define HAS_OUT}
{$define HAS_INT64}
{$define HAS_CARD32}
{$define StrictLong}
{$define HAS_ASSERT}
{$define DEFAULT}
{$define HAS_OVERLOAD}
{$endif}
{$ifdef WIN32}
{$define WIN32or64}
{$ifndef VirtualPascal}
{$define APPCONS}
{$endif}
{$endif}
{$ifdef WIN64}
{$define BIT64}
{$define WIN32or64}
{$define EXT64}
{$define APPCONS}
{$endif}
{$ifdef BIT64}
{$undef BASM}
{$endif}
{-- Default options --}
{$ifndef FPC}
{$B-} {short-circuit boolean expression evaluation, FPC has always B-!}
{$endif}
{$ifdef FPC}
{$ifdef CPUI386}
{$ASMmode intel}
{$endif}
{$goto on}
{$endif}
{$ifdef VirtualPascal}
{$ifndef debug}
{&Optimise+,SmartLink+,Speed+}
{$endif}
{$endif}
{$ifdef G_OPT}
{$G+}
{$endif}
{$ifdef Q_OPT}
{Most Crypto and CRC/Hash units need Q-, define Q+ locally if needed}
{$Q-}
{$endif}
{$ifdef debug}
{$R+,S+} {Note: D9+ needs $R- for StrictLong setting!}
{$else}
{$R-,S-}
{$endif}
{$ifdef SIMULATE_EXT64}
{$define EXT64}
{$endif}
{$ifdef BIT16}
{$F-}
{$endif}
{-- Record the starting values of important local options --}
{$ifopt A+} {$define Align_on} {$endif}
{$ifopt B+} {$define BoolEval_on} {$endif}
{$ifopt D+} {$define DebugInfo_on} {$endif}
{$ifopt I+} {$define IOChecks_on} {$endif}
{$ifopt R+} {$define RangeChecks_on} {$endif}
{$ifopt V+} {$define VarStringChecks_on} {$endif}
{$ifdef Q_OPT}
{$ifopt P+} {$define OpenStrings_on} {$endif}
{$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}
{$warn SYMBOL_LIBRARY OFF}
{$warn UNIT_DEPRECATED OFF}
{$warn UNIT_LIBRARY OFF}
{$warn UNIT_PLATFORM OFF}
{$ifdef HAS_UNSAFE}
{$warn UNSAFE_TYPE OFF}
{$warn UNSAFE_CODE OFF}
{$warn UNSAFE_CAST OFF}
{$endif}
{$endif}
{$else}
{$ifdef HAS_MSG}
{$message 'std.inc included more than once'}
{$endif}
{$endif}

View File

@ -0,0 +1,5 @@
These files are copies from the FCL sources of FPC 3.3+ so that
laz_fpspreadsheet_crypto can be compiled with older FPC versions.
Note: the files were renamed to prefix "fps" rather than "fp" so that the
original FCL units can be used automatically if FPC version is v3.3 or newer.

View File

@ -0,0 +1,469 @@
{ This file is part of the Free Component Library.
Hash unit utilities.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
{
Part of this code is based on earlier work by Wolfgang Erhardt.
}
unit fpsHashUtils;
{$mode ObjFPC}{$H+}
{$modeswitch advancedrecords}
interface
uses
SysUtils;
Type
EHashUtil = Class(Exception);
Procedure BytesFromVar(out aBytes : TBytes; aLocation : Pointer; aSize : Integer);
Function BytesFromVar(aLocation : Pointer; aSize : Integer) : TBytes;
Procedure BytesToVar(const aBytes : TBytes; out aLocation; aSize : Integer);
Procedure BytesToVar(const aBytes : TBytes; out aLocation : Pointer);
Procedure HexStrToBytes(Const aHexStr : String; out aBytes : TBytes); overload;
Function HexStrToBytes(Const aHexStr : String) : TBytes; overload;
Function HexStrToString(Const aHexStr : String) : String; overload;
Function BytesToHexStr(Const aSource : AnsiString) : Ansistring; overload;
Procedure BytesToHexStr(out aHexStr : AnsiString;Const aSource : AnsiString); overload;
Procedure BytesToHexStr(out aHexStr : AnsiString; aBytes : PByte; aSize : Integer); overload;
Procedure BytesToHexStr(out aHexStr : AnsiString; aBytes : TBytes); overload;
Function BytesToHexStr(aBytes : TBytes) : AnsiString; overload;
Procedure BytesToHexStrAppend(aBytes : TBytes;var aHexStr : AnsiString);
Function StringToHex(const s: string): string; overload;
Procedure BytesEncodeBase64(Source: Tbytes; out Dest: AnsiString; const IsURL, MultiLines, Padding: Boolean);
Function BytesEncodeBase64(Source: Tbytes; const IsURL, MultiLines, Padding: Boolean) : AnsiString;
Function BytesToStr(const aBytes: TBytes): string; overload;
Function CryptoGetRandomBytes(Buffer: PByte; const Count: Integer; ZeroBytesAllowed: boolean = true): Boolean;
Function ExtractBetween(const ASource,aStart,aEnd : String) : String;
Type
TGetRandomBytes = function(aBytes : PByte; aCount: Integer): Boolean;
var
GetRandomBytes : TGetRandomBytes;
implementation
Const
HexDigits: Array[0..15] of AnsiChar = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
procedure BytesFromVar(out aBytes: TBytes; aLocation: Pointer; aSize: Integer);
begin
aBytes:=[];
SetLength(aBytes,aSize);
if aSize>0 then
Move(aLocation^,aBytes[0],aSize);
end;
function BytesFromVar(aLocation: Pointer; aSize: Integer): TBytes;
begin
BytesFromVar(Result,aLocation,aSize);
end;
procedure BytesToVar(const aBytes: TBytes; out aLocation; aSize: Integer);
begin
if aSize>Length(aBytes) then
aSize:=Length(aBytes);
Move(aBytes[0],aLocation,aSize);
end;
procedure BytesToVar(const aBytes: TBytes; out aLocation: Pointer);
begin
BytesToVar(aBytes,aLocation,Length(aBytes));
end;
function HexStrToBytes(const aHexStr: String): TBytes;
begin
Result:=[];
HexStrToBytes(aHexStr,Result);
end;
function HexStrToString(const aHexStr: String): String;
var
aBytes: TBytes;
l: SizeInt;
begin
aBytes:=[];
HexStrToBytes(aHexStr,aBytes);
l:=length(aBytes);
if l=0 then exit;
SetLength(Result{%H-},l);
Move(aBytes[0],Result[1],l);
end;
procedure HexStrToBytes(const aHexStr: String; out aBytes: TBytes);
const
Convert: array['0'..'f'] of SmallInt =
( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15);
Var
Len : LongInt;
P: PAnsiChar;
PResult: PByte;
begin
Len:=Length(aHexStr);
aBytes:=[];
SetLength(aBytes, (Len+1) div 2);
if Len=0 then Exit;
P := PAnsiChar(aHexStr);
PResult := PByte(aBytes);
while Len > 0 do
begin
PResult^ := (Convert[P[0]] shl 4) + Convert[P[1]];
Inc(PResult);
Inc(P, 2);
Dec(Len, 2);
end;
end;
function BytesToHexStr(const aSource: AnsiString): Ansistring;
begin
BytesToHexStr(Result,aSource);
end;
procedure BytesToHexStr(out aHexStr: AnsiString; const aSource: AnsiString);
begin
BytesToHexStr(aHexStr,PByte(PChar(aSource)),Length(aSource))
end;
procedure BytesToHexStr(out aHexStr : AnsiString; aBytes : PByte; aSize : Integer);
var
I: Integer;
PB : Pbyte;
PC : PAnsiChar;
begin
aHexStr:='';
if aSize=0 then
exit;
SetLength(aHexStr,aSize*2);
PB:=aBytes;
PC:=PChar(aHexStr);
for I:=0 to aSize-1 do
begin
PC^:=HexDigits[PB^ shr 4];
Inc(PC);
PC^:=HexDigits[PB^ and $f];
Inc(PC);
Inc(PB);
end;
end;
procedure BytesToHexStr(out aHexStr: AnsiString; aBytes: TBytes);
begin
BytesToHexStr(aHexStr,PByte(aBytes),Length(aBytes));
end;
function BytesToHexStr(aBytes: TBytes): AnsiString;
begin
BytesToHexStr(Result,aBytes);
end;
procedure BytesToHexStrAppend(aBytes: TBytes; var aHexStr: AnsiString);
begin
aHexStr:=aHexStr+BytesToHexStr(aBytes);
end;
function StringToHex(const s: string): string;
begin
if s='' then exit;
BytesToHexStr(Result,@s[1],length(s));
end;
function GetBase64EncodedSize(const SourceSize: Int32; const MultiLines: Boolean): Int32;
var
Lines: Int32;
begin
Result := (SourceSize div 3) * 4;
if SourceSize mod 3 > 0 then
Inc(Result, 4);
if MultiLines then
begin
Lines := Result div 76;
Inc(Result, Lines*2); // #13#10 for each lines
end;
end;
procedure BytesEncodeBase64(Source: Tbytes; out Dest: AnsiString; const IsURL, MultiLines, Padding: Boolean);
const
FCodingTable: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
FCodingTableURL: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
procedure XBufferEncode64_1(var DestBuf: PByte; const AIn1: Byte; const IsURL: Boolean); inline;
begin
if IsURL then
begin
DestBuf[0] := Ord(FCodingTableURL[((AIn1 shr 2) and 63) + 1]);
DestBuf[1] := Ord(FCodingTableURL[((AIn1 shl 4) and 63) + 1]);
end else begin
DestBuf[0] := Ord(FCodingTable[((AIn1 shr 2) and 63) + 1]);
DestBuf[1] := Ord(FCodingTable[((AIn1 shl 4) and 63) + 1]);
end;
Inc(DestBuf,2);
end;
procedure XBufferEncode64_2(var DestBuf: PByte; const AIn1, AIn2: Byte; const IsURL: Boolean); inline;
begin
if IsURL then
begin
DestBuf[0] := Ord(FCodingTableURL[((AIn1 shr 2) and 63) + 1]);
DestBuf[1] := Ord(FCodingTableURL[(((AIn1 shl 4) or (AIn2 shr 4)) and 63) + 1]);
DestBuf[2] := Ord(FCodingTableURL[((AIn2 shl 2) and 63) + 1]);
end else begin
DestBuf[0] := Ord(FCodingTable[((AIn1 shr 2) and 63) + 1]);
DestBuf[1] := Ord(FCodingTable[(((AIn1 shl 4) or (AIn2 shr 4)) and 63) + 1]);
DestBuf[2] := Ord(FCodingTable[((AIn2 shl 2) and 63) + 1]);
end;
Inc(DestBuf,3);
end;
procedure XBufferEncode64_3(var DestBuf: PByte; const AIn1, AIn2, AIn3: Byte; const IsURL: Boolean); inline;
begin
if IsURL then
begin
DestBuf[0] := Ord(FCodingTableURL[((AIn1 shr 2) and 63) + 1]);
DestBuf[1] := Ord(FCodingTableURL[(((AIn1 shl 4) or (AIn2 shr 4)) and 63) + 1]);
DestBuf[2] := Ord(FCodingTableURL[(((AIn2 shl 2) or (AIn3 shr 6)) and 63) + 1]);
DestBuf[3] := Ord(FCodingTableURL[(Ord(AIn3) and 63) + 1]);
end else begin
DestBuf[0] := Ord(FCodingTable[((AIn1 shr 2) and 63) + 1]);
DestBuf[1] := Ord(FCodingTable[(((AIn1 shl 4) or (AIn2 shr 4)) and 63) + 1]);
DestBuf[2] := Ord(FCodingTable[(((AIn2 shl 2) or (AIn3 shr 6)) and 63) + 1]);
DestBuf[3] := Ord(FCodingTable[(Ord(AIn3) and 63) + 1]);
end;
Inc(DestBuf,4);
end;
var
BufSize, BufSize3: Int32;
Ch1, Ch2, Ch3: Byte;
DestBuf, SourceBuf: PByte;
DestCapacity, DestSize: Int32;
Index, IndexCRLF : Int32;
begin
BufSize := Length(Source);
if BufSize = 0 then
Exit;
DestCapacity := GetBase64EncodedSize(BufSize, MultiLines);
DestSize := 0;
Dest:='';
SetLength(Dest, DestCapacity);
SourceBuf := PByte(Source);
DestBuf := PByte(Dest);
IndexCRLF := 0;
Index := 0;
BufSize3 := (BufSize div 3)*3;
while Index < BufSize3 do
begin // Process the buffer up to the trailing 2 chars
Ch1 := SourceBuf[0];
Ch2 := SourceBuf[1];
Ch3 := SourceBuf[2];
SourceBuf := Pointer(PtrUInt(SourceBuf)+3);
Inc(Index, 3);
XBufferEncode64_3(DestBuf, Ch1, Ch2, Ch3, IsURL);
Inc(DestSize, 4);
if MultiLines then
begin
if (IndexCRLF = 18) and (Index < BufSize3) then // KW 20170405 BufSize -> BufSize3
begin
DestBuf[0] := Ord(#13);
DestBuf[1] := Ord(#10);
DestBuf := Pointer(PtrUInt(DestBuf)+2);
Inc(DestSize, 2);
IndexCRLF := 0;
end else
Inc(IndexCRLF);
end;
end;
if MultiLines and (IndexCRLF = 19) and (Index < BufSize) then // KW 20170405 IndexCRLF=18 -> 19
begin
DestBuf[0] := Ord(#13);
DestBuf[1] := Ord(#10);
DestBuf := Pointer(PtrUInt(DestBuf)+2);
Inc(DestSize, 2);
end;
if Index = BufSize-2 then // Last remaining 2 chars
begin
Ch1 := SourceBuf[0];
Ch2 := SourceBuf[1];
XBufferEncode64_2(DestBuf, Ch1, Ch2, IsURL);
Inc(DestSize, 3);
if Padding then
begin
DestBuf[0] := Ord('=');
Inc(DestSize);
end;
end else if Index = BufSize-1 then // Last remaining char
begin
Ch1 := Source[Index];
XBufferEncode64_1(DestBuf, Ch1, IsURL);
Inc(DestSize, 2);
if Padding then
begin
DestBuf[0] := Ord('=');
DestBuf[1] := Ord('=');
Inc(DestSize, 2);
end;
end;
SetLength(Dest,DestSize);
end;
function BytesEncodeBase64(Source: Tbytes; const IsURL, MultiLines,
Padding: Boolean): AnsiString;
begin
BytesEncodeBase64(Source,Result,IsURL, MultiLines, Padding);
end;
function BytesToStr(const aBytes: TBytes): string;
begin
SetLength(Result{%H-},length(aBytes));
if aBytes=nil then exit;
Move(aBytes[0],Result[1],length(aBytes));
end;
type
TUInt32 = Cardinal;
PUInt32Array = ^TUInt32;
TLecuyer = record
rs1, rs2, rs3: UInt32;
SeedCount: UInt32;
ZeroBytesAllowed: boolean;
procedure Seed;
function Next: UInt32;
end;
procedure TLecuyer.Seed;
var
VLI: Array[0..2] of byte;
I : Integer;
begin
I:=0;
Repeat
Inc(I);
if (Pointer(GetRandomBytes)=Nil) or not GetRandomBytes(@VLI,Sizeof(VLI)) then
Raise EHashUtil.Create('Cannot seed Lecuyer: no random bytes');
rs1 := VLI[0];
rs2 := VLI[1];
rs3 := VLI[2];
Until ((rs1>1) and (rs2>7) and (rs3>15)) or (I>100);
if I>100 then
Raise EHashUtil.Create('Cannot seed Lecuyer: no suitable random bytes');
SeedCount := 1;
end;
function TLecuyer.Next: UInt32;
begin
repeat
if SeedCount and $FFFF = 0 then // reseed after 256KB of output
Seed
else
Inc(SeedCount);
Result := rs1;
rs1 := ((Result and -2) shl 12) xor (((Result shl 13) xor Result) shr 19);
Result := rs2;
rs2 := ((Result and -8) shl 4) xor (((Result shl 2) xor Result) shr 25);
Result := rs3;
rs3 := ((Result and -16) shl 17) xor (((Result shl 3) xor Result) shr 11);
Result := rs1 xor rs2 xor Result;
if ZeroBytesAllowed then exit;
if ((Result and $ff)<>0)
and ((Result and $ff00)<>0)
and ((Result and $ff0000)<>0)
and ((Result and $ff000000)<>0) then
exit;
until false;
end;
function CryptoGetRandomBytes(Buffer: PByte; const Count: Integer;
ZeroBytesAllowed: boolean): Boolean;
var
I, Remainder, Rounds: Integer;
Lecuyer: TLecuyer;
R: UInt32;
begin
Result := True;
Lecuyer.ZeroBytesAllowed:=ZeroBytesAllowed;
Lecuyer.Seed;
Rounds := Count div SizeOf(UInt32);
for I := 0 to Rounds-1 do
PUInt32Array(Buffer)[I] := Lecuyer.Next;
Remainder := Count mod SizeOf(UInt32);
if Remainder > 0 then
begin
R := Lecuyer.Next;
Move(R, Buffer[Rounds*SizeOf(UInt32)], Remainder);
R:=R-R;// Silence compiler warning
end;
end;
function ExtractBetween(const ASource, aStart, aEnd: String): String;
Var
P1,P2 : Integer;
begin
Result:='';
P1:=Pos(aStart,ASource);
if P1<=0 then exit;
Inc(P1,Length(aStart));
P2:=Pos(aEnd,ASource,P1);
if P2<=0 then exit;
Result:=Copy(aSource,P1,P2-P1);
end;
function IntGetRandomNumber(aBytes : PByte; aCount: Integer): Boolean;
Var
i: Integer;
P : PByte;
begin
P:=aBytes;
i:=0;
while i<aCount do
begin
P^:=Random(256);
Inc(P);
inc(i);
end;
Result:=True;
end;
begin
GetRandomBytes:=@IntGetRandomNumber;
end.

View File

@ -0,0 +1,434 @@
{
This file is part of the Free Component Library.
Copyright (c) 2021 by the Free Pascal team.
SHA256 and HMACSha256 routines.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
unit fpsSHA256;
{$mode ObjFPC}{$H+}
{$MODESWITCH advancedrecords}
interface
uses
Classes, SysUtils;
Type
TSHA256Digest = packed array[0..31] of Byte;
PSHA256Digest = ^TSHA256Digest;
PSHA256 = ^TSHA256;
TSHA256 = record
Context: array[0..7] of UInt32;
Digest: TSHA256Digest;
HashBuffer: array[0..63] of Byte;
Index: UInt32;
TotalLength: Int64;
procedure Compress;
procedure Final;
procedure Init;
function IsEqual(const ADigest: TSHA256Digest): Boolean;
procedure OutputHexa(out Result: AnsiString);
procedure Update(PBuf: PByte; Size: UInt32); overload;
procedure Update(const Value: TBytes); overload;
// Calculate SHA256, return digest as bytes.
class procedure DigestBytes(const Value: TBytes; out Result: TBytes) ; static;
// Calculate SHA256, return digest as base64(url) string
class procedure DigestBase64(const Value: TBytes; const IsURL: Boolean; out Result: AnsiString); static;
// Calculate SHA256, return digest as HEX encoded string
class procedure DigestHexa(const Value: TBytes; out Result: AnsiString); static;
// HMAC using SHA256 as hash
Class function HMAC(Key: PByte; KeySize: UInt32; Data: PByte; DataSize: UInt32; var aDigest: TSHA256Digest): Boolean; overload; static;
class function HMAC(Key: PByte; KeySize: UInt32; Data: PByte; DataSize: UInt32; Data2: PByte; DataSize2: UInt32; Data3: PByte; DataSize3: UInt32; var aDigest: TSHA256Digest): Boolean; overload; static;
// Calculate HMacSHA256, return digest as hex string.
class function HMACHexa(const Key, Data: TBytes; out SignatureHexa: AnsiString): Boolean; overload; static;
// Calculate SHA256 from a stream, return digest.
class procedure Stream(aStream: TStream; out aDigest: TSHA256Digest); static; overload;
class function Stream(aStream: TStream): TSHA256Digest; static; overload;
// Digest Stream, result as HexaDecimal string.
class procedure StreamHexa(aStream: TStream; out Result: AnsiString); static; overload;
class Function StreamHexa(aStream: TStream): AnsiString; static overload;
// Digest Stream, result as Base64-encoded string
class procedure StreamBase64(aStream: TStream; isURL : Boolean; out Result: AnsiString); static; overload;
class Function StreamBase64(aStream: TStream; isURL : Boolean): AnsiString; static; overload;
// HKDF : Derive key of desired length from a salt,input key and info (RF5869, using HMACSHA256) .
class function HKDF(const Salt, IKM, Info: TBytes; var Output: TBytes; const DesiredLen: Integer): Boolean; static;
end;
Const
SHA256_DIGEST_SIZE = SizeOf(TSHA256Digest); // 32
implementation
uses fpsHashUtils;
//------------------------------------------------------------------------------
// SHA256
//------------------------------------------------------------------------------
procedure TSHA256.Init;
begin
Self.Index := 0;
Self.TotalLength := 0;
FillChar(Self.HashBuffer, Sizeof(Self.HashBuffer), 0);
Self.Context[0] := $6a09e667;
Self.Context[1] := $bb67ae85;
Self.Context[2] := $3c6ef372;
Self.Context[3] := $a54ff53a;
Self.Context[4] := $510e527f;
Self.Context[5] := $9b05688c;
Self.Context[6] := $1f83d9ab;
Self.Context[7] := $5be0cd19;
end;
procedure TSHA256.Compress;
// Actual hashing function
const
K: array[0..63] of UInt32 = (
$428a2f98, $71374491, $b5c0fbcf, $e9b5dba5, $3956c25b, $59f111f1,
$923f82a4, $ab1c5ed5, $d807aa98, $12835b01, $243185be, $550c7dc3,
$72be5d74, $80deb1fe, $9bdc06a7, $c19bf174, $e49b69c1, $efbe4786,
$0fc19dc6, $240ca1cc, $2de92c6f, $4a7484aa, $5cb0a9dc, $76f988da,
$983e5152, $a831c66d, $b00327c8, $bf597fc7, $c6e00bf3, $d5a79147,
$06ca6351, $14292967, $27b70a85, $2e1b2138, $4d2c6dfc, $53380d13,
$650a7354, $766a0abb, $81c2c92e, $92722c85, $a2bfe8a1, $a81a664b,
$c24b8b70, $c76c51a3, $d192e819, $d6990624, $f40e3585, $106aa070,
$19a4c116, $1e376c08, $2748774c, $34b0bcb5, $391c0cb3, $4ed8aa4a,
$5b9cca4f, $682e6ff3, $748f82ee, $78a5636f, $84c87814, $8cc70208,
$90befffa, $a4506ceb, $bef9a3f7, $c67178f2);
Type
TBuf64 = array[0..63] of UInt32;
var
A, B, C, D, E, F, G, H: UInt32;
W: TBuf64;
I: UInt32;
t1, t2: UInt32;
begin
w:=Default(TBuf64);
// Calculate "expanded message blocks"
Move(HashBuffer, W, Sizeof(HashBuffer));
for I := 0 to 15 do
W[I] := SwapEndian(W[I]);
for I := 16 to 63 do
W[I] := (((W[I-2] shr 17) or(W[I-2] shl 15)) xor ((W[I-2] shr 19) or (W[I-2] shl 13))
xor (W[I-2] shr 10))+W[I-7]+(((W[I-15] shr 7) or (W[I-15] shl 25))
xor ((W[I-15] shr 18) or (W[I-15] shl 14)) xor (W[I-15] shr 3))+W[I-16];
A := Context[0]; B := Context[1]; C := Context[2]; D := Context[3]; E := Context[4]; F := Context[5]; G := Context[6]; H := Context[7];
for I := 0 to High(W) do
begin
t1 := H+(((E shr 6) or (E shl 26)) xor ((E shr 11) or (E shl 21)) xor ((E shr 25) or (E shl 7)))+((E and F) xor (not E and G))+K[I]+W[I];
t2 := (((A shr 2) or (A shl 30)) xor ((A shr 13) or (A shl 19)) xor ((A shr 22) xor (A shl 10)))+((A and B) xor (A and C) xor (B and C));
H := G; G := F; F := E; E := D+t1;
D := C; C := B; B := A; A := t1+t2;
end;
Inc(Context[0], A);
Inc(Context[1], B);
Inc(Context[2], C);
Inc(Context[3], D);
Inc(Context[4], E);
Inc(Context[5], F);
Inc(Context[6], G);
Inc(Context[7], H);
end;
type
TInt64Rec = packed record
case Integer of
0: (Lo, Hi: UInt32);
1: (QuadPart: Int64);
end;
procedure TSHA256.Final;
begin
// 1. append bit '1' after Buffer
HashBuffer[Self.Index] := $80;
FillChar(HashBuffer[Self.Index+1], SizeOf(HashBuffer)-Self.Index-1, 0);
// 2. Compress if more than 448 bits, (no room for 64 bit length)
if Self.Index >= 56 then
begin
Compress;
FillChar(HashBuffer, SizeOf(HashBuffer), 0);
end;
// Write 64 bit Buffer length into the last bits of the last block
// (in big endian format) and do a final compress
PUInt32(@HashBuffer[56])^ := SwapEndian(TInt64Rec(TotalLength).Hi);
PUInt32(@HashBuffer[60])^ := SwapEndian(TInt64Rec(TotalLength).Lo);
Compress;
Context[0] := SwapEndian(Context[0]);
Context[1] := SwapEndian(Context[1]);
Context[2] := SwapEndian(Context[2]);
Context[3] := SwapEndian(Context[3]);
Context[4] := SwapEndian(Context[4]);
Context[5] := SwapEndian(Context[5]);
Context[6] := SwapEndian(Context[6]);
Context[7] := SwapEndian(Context[7]);
Move(Context, Digest, Sizeof(Context));
// Self.Init; // uncomment if you need security protection against memory inspection
end;
function TSHA256.IsEqual(const ADigest: TSHA256Digest): Boolean;
var
Left, Right: TBytes;
begin
Left:=BytesFromVar(@ADigest, SizeOf(ADigest));
Right:=BytesFromVar(@Self.Digest, SizeOf(Self.Digest));
Result:=CompareMem(Pointer(Left), Pointer(Right),Length(Left));
end;
procedure TSHA256.Update(PBuf: PByte; Size: UInt32);
var
Len: UInt32;
begin
Inc(TotalLength, Int64(UInt32(Size)) * 8);
while Size > 0 do
begin
if (Sizeof(HashBuffer)-Self.Index) <= UInt32(Size) then
begin
Len := Sizeof(HashBuffer)-Self.Index;
Move(PBuf^, HashBuffer[Self.Index], Len);
Dec(Size, Len);
Inc(PBuf, Len);
Compress;
Self.Index := 0;
end else
begin
Move(PBuf^, HashBuffer[Self.Index], Size);
Inc(Self.Index, Size);
Size := 0;
end;
end;
end;
procedure TSHA256.Update(const Value: TBytes);
begin
Update(PByte(Value), System.Length(Value));
end;
// @Result[64]
// 'abc' -> 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
procedure TSHA256.OutputHexa(out Result: AnsiString);
begin
BytesToHexStr(Result,PByte(@Self.Digest),SizeOf(Self.Digest));
end;
// @Result[32]
class procedure TSHA256.DigestBytes(const Value: TBytes; out Result: TBytes);
var
lSHA256: TSHA256;
begin
lSHA256.Init;
lSHA256.Update(Value);
lSHA256.Final;
BytesFromVar(Result, @lSHA256.Digest[0], SizeOf(lSHA256.Digest));
end;
class procedure TSHA256.DigestBase64(const Value: TBytes; const IsURL: Boolean; out Result: AnsiString);
var
S : TBytes;
lSHA256: TSHA256;
begin
lSHA256.Init;
lSHA256.Update(Value);
lSHA256.Final;
BytesFromVar(S, @lSHA256.Digest[0], SizeOf(lSHA256.Digest));
BytesEncodeBase64(S, Result, IsURL, False, False);
end;
// @Result[64]
Class procedure TSHA256.DigestHexa(const Value: TBytes; out Result: AnsiString);
var
SHA256: TSHA256;
begin
SHA256.Init;
SHA256.Update(Value);
SHA256.Final;
SHA256.OutputHexa(Result);
end;
class function TSHA256.HMAC(Key: PByte; KeySize: UInt32; Data: PByte; DataSize: UInt32; var aDigest: TSHA256Digest): Boolean;
begin
Result := HMAC(Key, KeySize, Data, DataSize, nil, 0, nil, 0, aDigest);
end;
{Generate a SHA256 HMAC (Hashed Message Authentication Code) using the Key and Data
The SHA256 HMAC algorithm is:
SHA256(Key xor oPad, SHA256(Key xor iPad, Data))
Where iPad is the byte $36 repeated 64 times
oPad is the byte $5c repeated 64 times
If Key is more than 64 bytes it will be hashed to Key = SHA256(Key) instead
If Key is less than 64 bytes it will be padded with zeros }
class function TSHA256.HMAC(Key: PByte; KeySize: UInt32; Data: PByte; DataSize: UInt32; Data2: PByte; DataSize2: UInt32; Data3: PByte; DataSize3: UInt32; var aDigest: TSHA256Digest): Boolean;
Type
TBuf64 = array[0..63] of Byte;
var
Count: UInt32;
KeyBuffer, PadBuffer: TBuf64;
SHA256, SHA256_: TSHA256;
begin
Result:=False;
if Key = nil then
Exit;
if Data = nil then
Exit;
KeyBuffer:=Default(TBuf64);
SHA256.Init;
if KeySize > 64 then
begin
SHA256.Update(Key, KeySize);
SHA256.Final;
System.Move(SHA256.Digest[0], KeyBuffer[0], SizeOf(SHA256.Digest));
end else
System.Move(Key^, KeyBuffer[0], KeySize);
// XOR the key buffer with the iPad value
for Count := 0 to 63 do
PadBuffer[Count] := KeyBuffer[Count] xor $36;
SHA256.Init;
SHA256.Update(@PadBuffer, SizeOf(PadBuffer));
SHA256.Update(Data, DataSize);
if Data2 <> nil then
SHA256.Update(Data2, DataSize2);
if Data3 <> nil then
SHA256.Update(Data3, DataSize3);
SHA256.Final;
// XOR the key buffer with the oPad value
for Count := 0 to 63 do
PadBuffer[Count] := KeyBuffer[Count] xor $5C;
// SHA256 the key buffer and the result of the inner SHA256 (Outer)
SHA256_.Init;
SHA256_.Update(@PadBuffer, SizeOf(PadBuffer));
SHA256_.Update(@SHA256.Digest, SizeOf(SHA256.Digest));
SHA256_.Final;
System.Move(SHA256_.Digest, aDigest, SizeOf(aDigest));
Result:=True;
end;
// @Result[64]
class function TSHA256.HMACHexa(const Key, Data: TBytes; out SignatureHexa: AnsiString): Boolean; overload;
var
aDigest: TSHA256Digest;
S: TBytes;
begin
aDigest:=Default(TSHA256Digest);
Result := HMAC(PByte(Key),Length(Key), PByte(Data), Length(Data), aDigest);
BytesFromVar(S, @aDigest[0], SizeOf(aDigest));
BytesToHexStr(SignatureHexa,S);
end;
class procedure TSHA256.Stream(aStream: TStream; out aDigest: TSHA256Digest);
const
BUFFER_SIZE = 64*1024;
var
aLen : LongInt;
Buffer: TBytes;
SHA256: TSHA256;
begin
Buffer:=Nil;
SHA256.Init;
SetLength(Buffer,BUFFER_SIZE);
repeat
aLen:=aStream.Read(Buffer, BUFFER_SIZE);
if aLen = 0 then
Break;
SHA256.Update(PByte(Buffer),aLen);
until aLen=0;
SHA256.Final;
aDigest:=SHA256.Digest;
end;
class function TSHA256.Stream(aStream: TStream): TSHA256Digest;
begin
Stream(aStream,Result);
end;
class procedure TSHA256.StreamHexa(aStream: TStream; out Result: AnsiString);
Var
B : TBytes;
aDigest : TSHA256Digest;
begin
Stream(aStream,aDigest);
BytesFromVar(B,@aDigest,SizeOf(TSHA256Digest));
BytesToHexStr(Result,B);
end;
class function TSHA256.StreamHexa(aStream: TStream): AnsiString;
begin
Result:='';
StreamHexa(aStream,Result);
end;
class procedure TSHA256.StreamBase64(aStream: TStream; isURL : Boolean; out Result: AnsiString);
Var
B : TBytes;
aDigest : TSHA256Digest;
begin
Stream(aStream,aDigest);
BytesFromVar(B,@aDigest,SizeOf(TSHA256Digest));
BytesEncodeBase64(B,Result,isUrl,False,False);
end;
class Function TSHA256.StreamBase64(aStream: TStream; isURL : Boolean): AnsiString;
begin
Result:='';
StreamBase64(aStream,isURL,Result);
end;
class function TSHA256.HKDF(const Salt, IKM, Info: TBytes; var Output: TBytes; const DesiredLen: Integer): Boolean;
var
PRK, T: TSHA256Digest;
Round: Byte;
begin
PRK:=Default(TSHA256Digest);
T:=Default(TSHA256Digest);
Result := HMAC(PByte(Salt), Length(Salt), PByte(IKM), Length(IKM), PRK);
if not Result then
Exit;
Round := 1;
while Length(Output) < DesiredLen do
begin
if Length(Output) = 0 then
Result := HMAC(@PRK, SizeOf(PRK), PByte(Info), Length(Info), @Round, SizeOf(Round), nil, 0, T)
else
Result := HMAC(@PRK, SizeOf(PRK), @T, SizeOf(T), PByte(Info), Length(Info), @Round, SizeOf(Round), T);
if not Result then
Exit;
Inc(Round);
Output:=Concat(OutPut,BytesFromVar(@T,SizeOf(T)));
if Length(Output) >= DesiredLen then
Break;
end;
SetLength(Output,DesiredLen);
end;
end.

View File

@ -0,0 +1,256 @@
{-------------------------------------------------------------------------------
Generic decryption procedures
--------------------------------------------------------------------------------
Use one of the defines to select the cryptographic library used.
- WOLFGANG_EHRHARDT_LIB:
these units are included in fpspreadsheet directly.
- DCPCRYPT:
the package DCPCrypt must be added to the "required packages" of
fpsSpreadsheet_crypto
-------------------------------------------------------------------------------}
unit fpsCryptoProc;
{$mode objfpc}{$H+}
{ Activate one of the following two defines. }
{$DEFINE WOLFGANG_EHRHARDT_LIB}
{.$DEFINE DCPCRYPT}
{$IF DEFINED(DCPCRYPT) AND DEFINED(WOLFGANG_EHRHARDT_LIB)}
ERROR: Only a single cryptographic library can be selected.
{$ENDIF}
interface
uses
Classes, SysUtils,
Base64, sha1,
{$IF FPC_FullVersion >= 30300}
fpHashUtils, fpSHA256,
{$ELSE}
fpsHashUtils, fpsSHA256,
{$ENDIF}
{$IFDEF WOLFGANG_EHRHARDT_LIB}
aes_type, aes_cbc, aes_ecb;
{$ENDIF}
{$IFDEF DCPCRYPT}
DCPrijndael;
{$ENDIF}
function Calc_SHA1(const AText: RawByteString): RawByteString;
function Calc_SHA256(AText: RawByteString): RawByteString;
function PBKDF2_HMAC_SHA1(pass, salt: RawByteString; count, kLen: Integer): RawByteString;
function Decrypt_AES_ECB(const Key; KeySizeBits: LongWord;
const InData; var OutData; DataSize: LongWord): String;
function DecryptStream_AES_ECB(const Key; KeySizeBits: LongWord;
ASrcStream, ADestStream: TStream; ASrcStreamSize: QWord): String;
implementation
uses
Math;
function Calc_SHA1(const AText: RawByteString): RawByteString;
var
sha1Digest: TSHA1Digest;
begin
sha1Digest := SHA1String(AText);
SetLength(Result, 20);
Move(sha1Digest[0], Result[1], 20);
end;
function Calc_SHA256(AText: RawByteString): RawByteString;
var
sha256: TSHA256;
begin
sha256.Init;
sha256.Update(@AText[1], Length(AText));
sha256.Final;
SetLength(Result, 32);
Move(sha256.Digest[0], Result[1], 32);
end;
function RPad(x: RawByteString; c: Char; s: Integer): RawByteString;
var
L: Integer;
begin
L := Length(x);
if L < s then
begin
SetLength(Result, s);
Move(x[1], Result[1], L);
FillChar(Result[L+1], s-L, c);
end else
Result := x;
end;
function Fill(c: Char; Len: Integer): RawByteString; inline;
begin
SetLength(Result, Len);
FillChar(Result[1], Len, c);
end;
function XorBlock(s, x: RawByteString): RawByteString; inline;
var
L, i: Integer;
Ps, Px: PByte;
begin
L := Length(s);
SetLength(Result, L);
Ps := PByte(@s[1]);
Px := PByte(@x[1]);
for i := 1 to L do
begin
Result[i] := Char(Ps^ xor Px^);
inc(Ps);
inc(Px);
end;
end;
function Calc_HMAC_SHA1(message, key: RawByteString): RawByteString;
const
blockSize = 64;
begin
if Length(key) > blocksize then
key := Calc_SHA1(key);
key := RPad(key, #0, blocksize);
Result := Calc_SHA1(XorBlock(key, Fill(#$36, blocksize)) + message);
Result := Calc_SHA1(XorBlock(key, Fill(#$5c, blocksize)) + Result);
end;
// https://keit.co/dcpcrypt-hmac-rfc2104/
function PBKDF2_HMAC_SHA1(pass, salt: RawByteString; count, kLen: Integer): RawByteString;
function IntX(i: Integer): RawByteString;
type
Int4 = record
i24, i16, i8, i0: char;
end;
begin
SetLength(Result, 4);
Result[1] := Int4(i).i0;
Result[2] := Int4(i).i8;
Result[3] := Int4(i).i16;
Result[4] := Int4(i).i24;
end;
var
D, I, J: Integer;
T, F, U: RawByteString;
begin
T := '';
D := Ceil(kLen / 20); //(hash.GetHashSize div 8));
for i := 1 to D do
begin
F := Calc_HMAC_SHA1(salt + IntX(i), pass);
U := F;
for j := 2 to count do
begin
U := Calc_HMAC_SHA1(U, pass);
F := XorBlock(F, U);
end;
T := T + F;
end;
Result := Copy(T, 1, kLen);
end;
function Decrypt_AES_ECB(const Key; KeySizeBits: LongWord;
const InData; var OutData; DataSize: LongWord): String;
{$IFDEF WOLFGANG_EHRHARDT_LIB}
var
ctx: TAESContext;
err: Integer;
begin
err := AES_ECB_Init_Decr(Key, KeySizeBits, ctx{%H-});
if err <> 0 then
begin
Result := 'Decrypt init error ' + IntToStr(err);
exit;
end;
err := AES_ECB_Decrypt(@InData, @OutData, DataSize, ctx);
if err <> 0 then
begin
Result := 'Decrypt error: ' + IntToStr(err);
exit;
end;
end;
{$ENDIF}
{$IFDEF DCPCRYPT}
var
AES_Cipher: TDCP_rijndael;
begin
Result := ''; // Error message
AES_Cipher := TDCP_rijndael.Create(nil);
try
AES_Cipher.Init(Key, keySizeBits, nil);
AES_Cipher.DecryptECB(InData, OutData);
finally
AES_Cipher.Free;
end;
end;
{$ENDIF}
function DecryptStream_AES_ECB(const Key; KeySizeBits: LongWord;
ASrcStream, ADestStream: TStream; ASrcStreamSize: QWord): String;
var
{$IFDEF WOLFGANG_EHRHARDT_LIB}
ctx: TAESContext;
{$ENDIF}
{$IFDEF DCPCRYPT}
AES_Cipher: TDCP_rijndael;
{$ENDIF}
keySizeBytes: Integer;
inData: TBytes = nil;
outData: TBytes = nil;
begin
Result := '';
keySizeBytes := KeySizeBits div 8;
SetLength(inData, keySizeBytes);
SetLength(outData, keySizeBytes);
{$IFDEF WOLFGANG_EHRHARDT_LIB}
AES_ECB_Init_Decr(Key, KeySizeBits, ctx);
{$ENDIF}
{$IFDEF DCPCRYPT}
AES_Cipher := TDCP_rijndael.Create(nil);
try
AES_Cipher.Init(Key, KeySizeBits, nil);
{$ENDIF}
while ASrcStreamSize > 0 do
begin
ASrcStream.ReadBuffer(inData[0], keySizeBytes);
{$IFDEF WOLFGANG_EHRHARDT_LIB}
AES_ECB_Decrypt(@inData[0], @outData[0], keySizeBytes, ctx);
{$ENDIF}
{$IFDEF DCPCRYPT}
AES_Cipher.DecryptECB(inData[0], outData[0]);
{$ENDIF}
if ASrcStreamSize < keySizeBytes then
ADestStream.WriteBuffer(outData[0], ASrcStreamSize) // Last block less then key size
else
ADestStream.WriteBuffer(outData[0], keySizeBytes);
if ASrcStreamSize < keySizeBytes then
ASrcStreamSize := 0
else
Dec(ASrcStreamSize, keySizeBytes);
end;
{$IFDEF DCPCRYPT}
finally
AES_Cipher.Free;
end;
{$ENDIF}
end;
end.

View File

@ -11,11 +11,8 @@ unit xlsxdecrypter;
interface
uses
Classes
, SysUtils
, sha1
, DCPrijndael
;
Classes, SysUtils, sha1,
fpsCryptoProc;
const
CFB_Signature = $E11AB1A1E011CFD0; // Compound File Binary Signature
@ -285,8 +282,6 @@ function TExcelFileDecryptor.CheckPasswordInternal(APassword: UnicodeString): Bo
const
Zero: DWord = 0;
var
AES_Cipher: TDCP_rijndael;
ConcArr : TBytes = nil;
LastHash: TSHA1Digest;
@ -373,16 +368,14 @@ begin
// 1. Encryption key is FEncryptionKey
// 2. Decrypt the EncryptedVerifier
AES_Cipher := TDCP_rijndael.Create(nil);
AES_Cipher.Init( FEncryptionKey[0], FEncInfo.Header.KeySize, nil );
AES_Cipher.DecryptECB(FEncInfo.Verifier.EncryptedVerifier[0] , {%H-}Verifier[0]);
Decrypt_AES_ECB(FEncryptionKey[0], FEncInfo.Header.KeySize,
FEncInfo.Verifier.EncryptedVerifier[0], Verifier[0], 16);
// 3. Decrypt the DecryptedVerifierHash
AES_Cipher.Burn;
AES_Cipher.Init( FEncryptionKey[0], FEncInfo.Header.KeySize, nil );
AES_Cipher.DecryptECB(FEncInfo.Verifier.EncryptedVerifierHash[0] , {%H-}VerifierHash[0]);
AES_Cipher.DecryptECB(FEncInfo.Verifier.EncryptedVerifierHash[16], VerifierHash[16]);
AES_Cipher.Free;
Decrypt_AES_ECB(FEncryptionKey[0], FEncInfo.Header.KeySize,
FEncInfo.Verifier.EncryptedVerifierHash[0], VerifierHash[0], 16);
Decrypt_AES_ECB(FEncryptionKey[0], FEncInfo.Header.KeySize,
FEncInfo.Verifier.EncryptedVerifierHash[16], VerifierHash[16], 16);
// 4. Calculate SHA1(Verifier)
LastHash := SHA1Buffer(Verifier[0], Length(Verifier));
@ -394,29 +387,32 @@ end;
function TExcelFileDecryptor.Decrypt(inFileName: string; outStream: TStream
): string;
begin
Result := Decrypt(inFileName, outStream, 'VelvetSweatshop' );
Result := Decrypt(inFileName, outStream, 'VelvetSweatshop');
end;
function TExcelFileDecryptor.Decrypt(inFileName: string; outStream: TStream;
APassword: UnicodeString): string;
Var
inStream : TFileStream;
inStream: TFileStream;
begin
if not FileExists(inFileName) then
Exit( inFileName + ' not found.' );
begin
Result := inFileName + ' not found.';
Exit;
end;
try
inStream := TFileStream.Create( inFileName, fmOpenRead );
inStream := TFileStream.Create(inFileName, fmOpenRead);
inStream.Position := 0;
Result := Decrypt( inStream, outStream, APassword );
Result := Decrypt(inStream, outStream, APassword);
finally
inStream.Free;
end;
end;
function TExcelFileDecryptor.Decrypt(inStream: TStream; outStream: TStream
): string;
function TExcelFileDecryptor.Decrypt(inStream: TStream;
outStream: TStream): string;
begin
Result := Decrypt(inStream, outStream, 'VelvetSweatshop' );
end;
@ -427,13 +423,7 @@ var
OLEStream: TMemoryStream;
OLEStorage: TOLEStorage;
OLEDocument: TOLEDocument;
AES_Cipher : TDCP_rijndael;
inData : TBytes = nil;
outData : TBytes = nil;
StreamSize : QWord;
KeySizeByte: Integer;
Err : string;
begin
if (not Assigned(inStream)) or (not Assigned(outStream)) then
@ -460,45 +450,19 @@ begin
// Start decryption
OLEStream.Position:=0;
outStream.Position:=0;
StreamSize := OLEStream.ReadQWord;
KeySizeByte := FEncInfo.Header.KeySize div 8;
SetLength(inData, KeySizeByte);
SetLength(outData, KeySizeByte);
DecryptStream_AES_ECB(FEncryptionKey[0], FEncInfo.Header.KeySize,
OLEStream, outStream, StreamSize);
AES_Cipher := TDCP_rijndael.Create(nil);
AES_Cipher.Init( FEncryptionKey[0], FEncInfo.Header.KeySize, nil );
While StreamSize > 0 do
begin
OLEStream.ReadBuffer(inData[0], KeySizeByte);
AES_Cipher.DecryptECB(inData[0], outData[0]);
if StreamSize < KeySizeByte then
outStream.WriteBuffer(outData[0], StreamSize) // Last block less then key size
else
outStream.WriteBuffer(outData[0], KeySizeByte);
if StreamSize < KeySizeByte then
StreamSize := 0
else
Dec(StreamSize, KeySizeByte);
end;
AES_Cipher.Free;
/////
except
Err := 'EncryptedPackage not found';
end;
finally
if Assigned(OLEStorage) then
OLEStorage.Free;
OLEStorage.Free;
OLEStream.Free;
end;
Exit( Err );
Result := Err;
end;
function TExcelFileDecryptor.isEncryptedAndSupported(AFileName: string

View File

@ -42,6 +42,10 @@ begin
AStream.Free;
AStream := TMemoryStream.Create;
DecryptedStream.Position := 0;
TMemoryStream(decryptedStream).SaveToFile('decr.zip');
DecryptedStream.Position := 0;
AStream.CopyFrom(DecryptedStream, DecryptedStream.Size);
AStream.Position := 0;
finally