unit Anubis;
interface
uses
Windows, SysUtils, Classes,
btypes, anu_base,
anu_ctr, anu_cbc;
{$ALIGN ON}
type
TAnubisByteKey128 = array [0..15] of Byte;
TAnubisByteKey160 = array [0..19] of Byte;
TAnubisByteKey192 = array [0..23] of Byte;
TAnubisByteKey256 = array [0..31] of Byte;
TAnubisCharKey128 = array [0..15] of Char8;
TAnubisCharKey160 = array [0..19] of Char8;
TAnubisCharKey192 = array [0..23] of Char8;
TAnubisCharKey256 = array [0..31] of Char8;
TAnubisBlock = TANUBlock;
const
NULL_ANUBIS_BLOCK : TAnubisBlock = ($00,$00,$00,$00,
$00,$00,$00,$00,
$00,$00,$00,$00,
$00,$00,$00,$00);
function AnubisEncrypt (const aValue: String;
const aKey; const aKeyBits: WORD;
IV: TAnubisBlock) : String; overload;
function AnubisEncrypt (const aValue: String;
const aKey; const aKeyBits: WORD) : String; overload;
function AnubisDecrypt (const aValue: String;
const aKey; const aKeyBits: WORD;
IV: TAnubisBlock) : String; overload;
function AnubisDecrypt (const aValue: String;
const aKey; const aKeyBits: WORD) : String; overload;
function AnubisError (const anErrorCode: Integer) : String;
{$I 'anu_err.inc'}
implementation
function StrToHex (const aValue: String) : String;
var
I : Integer;
begin
Result := '';
for I := 1 to Length (aValue) do
begin
Result := Result + IntToHex ( Ord (aValue [i]), 2 );
end;
end;
function HexToStr (const aValue: String) : String;
var
I : Integer;
Buffer : String [3];
begin
Result := '';
I := 0;
while ( I < Length (aValue) ) do
begin
Inc (I);
if ( I mod 2 = 0 ) then
begin
Buffer := UpperCase ( Format ('$%s%s',[ aValue [I-1], aValue [i] ]) );
Result := Result + Char ( StrToInt (Buffer) );
end;
end;
end;
function AnubisError (const anErrorCode: Integer) : String;
begin
Result := '';
case anErrorCode of
-1 : Result := ERR_ANUBIS_INVALID_KEY_SIZE;
-2 : Result := ERR_ANUBIS_INVALID_MODE;
-3 : Result := ERR_ANUBIS_INVALID_LENGTH;
-4 : Result := ERR_ANUBIS_DATA_AFTER_SHORT_BLOCK;
-5 : Result := ERR_ANUBIS_MULTIPLE_INC_PROCS;
-6 : Result := ERR_ANUBIS_INVALID_POINTER;
-7 : Result := ERR_ANUBIS_EAX_INV_TEXT_LENGTH;
-8 : Result := ERR_ANUBIS_EAX_INV_TAG_LENGTH;
-9 : Result := ERR_ANUBIS_EAX_VERIFY_TAG;
-15 : Result := ERR_ANUBIS_CTR_SEEK_OFFSET;
-20 : Result := ERR_ANUBIS_INVALID_16BIT_LENGTH;
else Result := ERR_ANUBIS_UNKNOWN;
end;
end;
{ Cipher Text Counter }
function AnubisEncrypt (const aValue: String;
const aKey; const aKeyBits: WORD;
IV: TAnubisBlock) : String;
var
md : TANUContext;
buf : PChar;
err : Integer;
I : integer;
begin
Result := '';
err := 0;
try
buf := PChar (aValue);
err := ANU_CBC_Init_Encr (aKey,128,IV,md);
//err := ANU_CTR_Init (aKey,128,IV,md);
if ( err <> 0 ) then
raise Exception.Create ( AnubisError (err) );
err := ANU_CBC_Encrypt ( buf, buf, Length (buf), md );
//err := ANU_CTR_Encrypt ( buf, buf, Length (buf), md );
if ( err <> 0 ) then
raise Exception.Create ( AnubisError (err) );
finally
if ( err = 0 ) then
Result := StrToHex ( StrPas (buf) );
end;
end;
function AnubisEncrypt (const aValue: String;
const aKey; const aKeyBits: WORD) : String;
begin
Result := AnubisEncrypt (aValue,aKey,aKeyBits,NULL_ANUBIS_BLOCK);
end;
function AnubisDecrypt (const aValue: String;
const aKey; const aKeyBits: WORD;
IV: TAnubisBlock) : String;
var
md : TANUContext;
buf : PChar;
err : Integer;
begin
Result := '';
err := 0;
try
buf := PChar ( HexToStr (aValue) );
err := ANU_CBC_Init_Decr (aKey,128,IV,md);
//err := ANU_CTR_Init (aKey,128,IV,md);
if ( err <> 0 ) then
raise Exception.Create ( AnubisError (err) );
err := ANU_CBC_Decrypt ( buf, buf, Length (buf), md );
//err := ANU_CTR_Decrypt ( buf, buf, Length (buf), md );
if ( err <> 0 ) then
raise Exception.Create ( AnubisError (err) );
finally
if ( err = 0 ) then
Result := StrPas (buf);
end;
end;
function AnubisDecrypt (const aValue: String;
const aKey; const aKeyBits: WORD) : String;
begin
Result := AnubisDecrypt (aValue,aKey,aKeyBits,NULL_ANUBIS_BLOCK);
end;
end.