TSynaBlockCipher knows any block size now. (was hardwired 64-bit block)

git-svn-id: https://svn.code.sf.net/p/synalist/code/trunk@140 7c85be65-684b-0410-a082-b2ed4fbef004
This commit is contained in:
geby 2011-04-28 08:23:05 +00:00
parent c252dd707f
commit 10a105565d

View File

@ -1,9 +1,9 @@
{==============================================================================| {==============================================================================|
| Project : Ararat Synapse | 001.000.001 | | Project : Ararat Synapse | 001.001.000 |
|==============================================================================| |==============================================================================|
| Content: Encryption support | | Content: Encryption support |
|==============================================================================| |==============================================================================|
| Copyright (c)2007-2010, Lukas Gebauer | | Copyright (c)2007-2011, Lukas Gebauer |
| All rights reserved. | | All rights reserved. |
| | | |
| Redistribution and use in source and binary forms, with or without | | Redistribution and use in source and binary forms, with or without |
@ -33,7 +33,7 @@
| DAMAGE. | | DAMAGE. |
|==============================================================================| |==============================================================================|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).| | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
| Portions created by Lukas Gebauer are Copyright (c)2007-2010. | | Portions created by Lukas Gebauer are Copyright (c)2007-2011. |
| All Rights Reserved. | | All Rights Reserved. |
| Based on work of David Barton and Eric Young | | Based on work of David Barton and Eric Young |
|==============================================================================| |==============================================================================|
@ -69,7 +69,7 @@ uses
SysUtils, Classes, synautil; SysUtils, Classes, synautil;
type type
{:@abstract(Implementation of common routines for 64-bit block ciphers) {:@abstract(Implementation of common routines block ciphers (dafault size is 64-bits))
Do not use this class directly, use descendants only!} Do not use this class directly, use descendants only!}
TSynaBlockCipher= class(TObject) TSynaBlockCipher= class(TObject)
@ -79,6 +79,8 @@ type
IV, CV: AnsiString; IV, CV: AnsiString;
procedure IncCounter; procedure IncCounter;
public public
{:Returns cipher block size in bytes.}
function GetSize: byte; virtual;
{:Sets the IV to Value and performs a reset} {:Sets the IV to Value and performs a reset}
procedure SetIV(const Value: AnsiString); virtual; procedure SetIV(const Value: AnsiString); virtual;
{:Returns the current chaining information, not the actual IV} {:Returns the current chaining information, not the actual IV}
@ -484,12 +486,17 @@ begin
end; end;
{==============================================================================} {==============================================================================}
function TSynaBlockCipher.GetSize: byte;
begin
Result := 8;
end;
procedure TSynaBlockCipher.IncCounter; procedure TSynaBlockCipher.IncCounter;
var var
i: integer; i: integer;
begin begin
Inc(CV[8]); Inc(CV[GetSize]);
i:= 7; i:= GetSize -1;
while (i> 0) and (CV[i + 1] = #0) do while (i> 0) and (CV[i + 1] = #0) do
begin begin
Inc(CV[i]); Inc(CV[i]);
@ -508,7 +515,7 @@ end;
procedure TSynaBlockCipher.SetIV(const Value: AnsiString); procedure TSynaBlockCipher.SetIV(const Value: AnsiString);
begin begin
IV := PadString(Value, 8, #0); IV := PadString(Value, GetSize, #0);
Reset; Reset;
end; end;
@ -532,21 +539,23 @@ var
i: integer; i: integer;
s: ansistring; s: ansistring;
l: integer; l: integer;
bs: byte;
begin begin
Result := ''; Result := '';
l := Length(InData); l := Length(InData);
for i:= 1 to (l div 8) do bs := GetSize;
for i:= 1 to (l div bs) do
begin begin
s := copy(Indata, (i - 1) * 8 + 1, 8); s := copy(Indata, (i - 1) * bs + 1, bs);
s := XorString(s, CV); s := XorString(s, CV);
s := EncryptECB(s); s := EncryptECB(s);
CV := s; CV := s;
Result := Result + s; Result := Result + s;
end; end;
if (l mod 8)<> 0 then if (l mod bs)<> 0 then
begin begin
CV := EncryptECB(CV); CV := EncryptECB(CV);
s := copy(Indata, (l div 8) * 8 + 1, l mod 8); s := copy(Indata, (l div bs) * bs + 1, l mod bs);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
end; end;
@ -557,22 +566,24 @@ var
i: integer; i: integer;
s, temp: ansistring; s, temp: ansistring;
l: integer; l: integer;
bs: byte;
begin begin
Result := ''; Result := '';
l := Length(InData); l := Length(InData);
for i:= 1 to (l div 8) do bs := GetSize;
for i:= 1 to (l div bs) do
begin begin
s := copy(Indata, (i - 1) * 8 + 1, 8); s := copy(Indata, (i - 1) * bs + 1, bs);
temp := s; temp := s;
s := DecryptECB(s); s := DecryptECB(s);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
CV := Temp; CV := Temp;
end; end;
if (l mod 8)<> 0 then if (l mod bs)<> 0 then
begin begin
CV := EncryptECB(CV); CV := EncryptECB(CV);
s := copy(Indata, (l div 8) * 8 + 1, l mod 8); s := copy(Indata, (l div bs) * bs + 1, l mod bs);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
end; end;
@ -617,21 +628,23 @@ var
i: integer; i: integer;
s: AnsiString; s: AnsiString;
l: integer; l: integer;
bs: byte;
begin begin
Result := ''; Result := '';
l := Length(InData); l := Length(InData);
for i:= 1 to (l div 8) do bs := GetSize;
for i:= 1 to (l div bs) do
begin begin
CV := EncryptECB(CV); CV := EncryptECB(CV);
s := copy(Indata, (i - 1) * 8 + 1, 8); s := copy(Indata, (i - 1) * bs + 1, bs);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
CV := s; CV := s;
end; end;
if (l mod 8)<> 0 then if (l mod bs)<> 0 then
begin begin
CV := EncryptECB(CV); CV := EncryptECB(CV);
s := copy(Indata, (l div 8) * 8 + 1, l mod 8); s := copy(Indata, (l div bs) * bs + 1, l mod bs);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
end; end;
@ -642,22 +655,24 @@ var
i: integer; i: integer;
S, Temp: AnsiString; S, Temp: AnsiString;
l: integer; l: integer;
bs: byte;
begin begin
Result := ''; Result := '';
l := Length(InData); l := Length(InData);
for i:= 1 to (l div 8) do bs := GetSize;
for i:= 1 to (l div bs) do
begin begin
s := copy(Indata, (i - 1) * 8 + 1, 8); s := copy(Indata, (i - 1) * bs + 1, bs);
Temp := s; Temp := s;
CV := EncryptECB(CV); CV := EncryptECB(CV);
s := XorString(s, CV); s := XorString(s, CV);
Result := result + s; Result := result + s;
CV := temp; CV := temp;
end; end;
if (l mod 8)<> 0 then if (l mod bs)<> 0 then
begin begin
CV := EncryptECB(CV); CV := EncryptECB(CV);
s := copy(Indata, (l div 8) * 8 + 1, l mod 8); s := copy(Indata, (l div bs) * bs + 1, l mod bs);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
end; end;
@ -668,20 +683,22 @@ var
i: integer; i: integer;
s: AnsiString; s: AnsiString;
l: integer; l: integer;
bs: byte;
begin begin
Result := ''; Result := '';
l := Length(InData); l := Length(InData);
for i:= 1 to (l div 8) do bs := GetSize;
for i:= 1 to (l div bs) do
begin begin
CV := EncryptECB(CV); CV := EncryptECB(CV);
s := copy(Indata, (i - 1) * 8 + 1, 8); s := copy(Indata, (i - 1) * bs + 1, bs);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
end; end;
if (l mod 8)<> 0 then if (l mod bs)<> 0 then
begin begin
CV := EncryptECB(CV); CV := EncryptECB(CV);
s := copy(Indata, (l div 8) * 8 + 1, l mod 8); s := copy(Indata, (l div bs) * bs + 1, l mod bs);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
end; end;
@ -692,20 +709,22 @@ var
i: integer; i: integer;
s: AnsiString; s: AnsiString;
l: integer; l: integer;
bs: byte;
begin begin
Result := ''; Result := '';
l := Length(InData); l := Length(InData);
for i:= 1 to (l div 8) do bs := GetSize;
for i:= 1 to (l div bs) do
begin begin
Cv := EncryptECB(CV); Cv := EncryptECB(CV);
s := copy(Indata, (i - 1) * 8 + 1, 8); s := copy(Indata, (i - 1) * bs + 1, bs);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
end; end;
if (l mod 8)<> 0 then if (l mod bs)<> 0 then
begin begin
CV := EncryptECB(CV); CV := EncryptECB(CV);
s := copy(Indata, (l div 8) * 8 + 1, l mod 8); s := copy(Indata, (l div bs) * bs + 1, l mod bs);
s := XorString(s, CV); s := XorString(s, CV);
Result := Result + s; Result := Result + s;
end; end;
@ -717,22 +736,24 @@ var
i: integer; i: integer;
s: AnsiString; s: AnsiString;
l: integer; l: integer;
bs: byte;
begin begin
Result := ''; Result := '';
l := Length(InData); l := Length(InData);
for i:= 1 to (l div 8) do bs := GetSize;
for i:= 1 to (l div bs) do
begin begin
temp := EncryptECB(CV); temp := EncryptECB(CV);
IncCounter; IncCounter;
s := copy(Indata, (i - 1) * 8 + 1, 8); s := copy(Indata, (i - 1) * bs + 1, bs);
s := XorString(s, temp); s := XorString(s, temp);
Result := Result + s; Result := Result + s;
end; end;
if (l mod 8)<> 0 then if (l mod bs)<> 0 then
begin begin
temp := EncryptECB(CV); temp := EncryptECB(CV);
IncCounter; IncCounter;
s := copy(Indata, (l div 8) * 8 + 1, l mod 8); s := copy(Indata, (l div bs) * bs + 1, l mod bs);
s := XorString(s, temp); s := XorString(s, temp);
Result := Result + s; Result := Result + s;
end; end;
@ -744,22 +765,24 @@ var
s: AnsiString; s: AnsiString;
i: integer; i: integer;
l: integer; l: integer;
bs: byte;
begin begin
Result := ''; Result := '';
l := Length(InData); l := Length(InData);
for i:= 1 to (l div 8) do bs := GetSize;
for i:= 1 to (l div bs) do
begin begin
temp := EncryptECB(CV); temp := EncryptECB(CV);
IncCounter; IncCounter;
s := copy(Indata, (i - 1) * 8 + 1, 8); s := copy(Indata, (i - 1) * bs + 1, bs);
s := XorString(s, temp); s := XorString(s, temp);
Result := Result + s; Result := Result + s;
end; end;
if (l mod 8)<> 0 then if (l mod bs)<> 0 then
begin begin
temp := EncryptECB(CV); temp := EncryptECB(CV);
IncCounter; IncCounter;
s := copy(Indata, (l div 8) * 8 + 1, l mod 8); s := copy(Indata, (l div bs) * bs + 1, l mod bs);
s := XorString(s, temp); s := XorString(s, temp);
Result := Result + s; Result := Result + s;
end; end;
@ -769,7 +792,7 @@ constructor TSynaBlockCipher.Create(Key: AnsiString);
begin begin
inherited Create; inherited Create;
InitKey(Key); InitKey(Key);
IV := StringOfChar(#0, 8); IV := StringOfChar(#0, GetSize);
IV := EncryptECB(IV); IV := EncryptECB(IV);
Reset; Reset;
end; end;