× Components and Libraries for Algorithms Development, discussions, problems and suggestions

Question TLb3DES external SIGSEGV

  • Ermo
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
More
5 years 6 months ago #7108 by Ermo
TLb3DES external SIGSEGV was created by Ermo
Hi,

I'm trying to use TLb3DES crypto with cmCBC mode and it generates external SIGSEGV error in JoinBlock procedure (lbCipher.pas; line 771)
  procedure JoinBlock(const L, R : LongInt;  var Block : TDESBlock); register;
  asm
    push ebx
    mov  bh, al
    mov  bl, ah
    rol  ebx, 16
    shr  eax, 16
    mov  bh, al
    mov  bl, ah
    mov  [ecx+4], ebx  <- debugger shows this line
    mov  bh, dl
    mov  bl, dh
    rol  ebx, 16
    shr  edx, 16
    mov  bh, dl
    mov  bl, dh
    mov  [ecx], ebx
    pop  ebx
  end;

test code:

File Attachment:

File Name: appmain.zip
File Size:764 KB


test:
Hit "Set Key"
Hit "Encrypt" and you will see error

My setup:
CT 5.20
Windows 7 64bit
Attachments:

Please Log in or Create an account to join the conversation.

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Away
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
More
5 years 6 months ago #7109 by Sternas Stefanos
Replied by Sternas Stefanos on topic TLb3DES external SIGSEGV
Thanks Sir
please install CT 5.30 we have make a lot of changes to pl_OnGuard pkg
look and CodeOcean samples

PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

  • Ermo
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
More
5 years 6 months ago - 5 years 6 months ago #7128 by Ermo
Replied by Ermo on topic TLb3DES external SIGSEGV
Hi,
Yesterday I made a clean install to CT5.3 but this not helped me.
Actually, I found a solution how to fix error in lbcchiper.pas file:

1. add new type in encryptionblock types:
     TDesConverter = record
     case boolean of
        false: (Bytes: array [0..7] of byte);
        true: (DWords: array  [0..1] of dword)
   end;

2. replace some asm procedures with purepascal, ok purepascal is slower but this fix the problem

NOTE. I've modified only
{$IFDEF CPU32}
part of code.
  procedure SplitBlock(const Block : TDESBlock; var L, R : DWord);
  var
   Temp: TDesConverter;
   I: integer;
  begin
    for I := Low(Block) to High(Block) do
    Temp.Bytes[7-I] := Block[I];
    L:= Temp.DWords[1];
    R:= Temp.DWords[0];
  end;

  procedure JoinBlock(const L, R : LongInt; var Block : TDESBlock);
  var
     Temp: TDesConverter;
     I: integer;
  begin
       Temp.DWords[0] := DWord(L);
       Temp.DWords[1] := DWord(R);
       for I := Low(Block) to High(Block) do
           Block[I] := Temp.Bytes[7-I];
  end;

procedure XorMemPrim(var Mem1; const Mem2; Count : Cardinal);
var
   i: Integer;
   p1,p2: NativeInt;
begin
   p1 := NativeInt(@Mem1);
   p2 := NativeInt(@Mem2);
   for i := 1 to count div 4 do
   begin
     PDWord(p1)^ := PDWord(p1)^ xor PDWord(p2)^;
     p1 := p1 + 4;
     p2 := p2 + 4;
   end;
   for i:=1 to count mod 4 do
   begin
     PByte(p1)^ := PByte(p1)^ xor PByte(p2)^;
     p1 := p1 + 1;
     p2 := p2 + 1;
   end;
end;

function RolX(I, C : DWord): DWord;
begin
   Result := (I shl (C and 31)) or (I shr (32-(C and 31)));
end;



Now, 3DES works but it is not compatible with other 3DES encryption programs.
I tried two online 3des calculators:
www.emvlab.org/descalc/
and
extranet.cryptomathic.com/descalc/index

I modified CT code to get InitialVector filled with zeroes but online calculators can not decrypt my encryped mesages and my program can not decrypt
online encrypted messages. I don't know how exactly 3DES encryption works so I can't debug or fix a code. So any help is welcome.
Last edit: 5 years 6 months ago by Ermo.

Please Log in or Create an account to join the conversation.

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Away
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
More
5 years 6 months ago #7129 by Sternas Stefanos
Replied by Sternas Stefanos on topic TLb3DES external SIGSEGV
Please Sir
zip and attach lbCipher.pas

PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Away
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
More
5 years 6 months ago #7130 by Sternas Stefanos
Replied by Sternas Stefanos on topic TLb3DES external SIGSEGV
Sir
your patch work perfect :woohoo:

PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

  • Ermo
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
More
5 years 6 months ago - 5 years 6 months ago #7131 by Ermo
Replied by Ermo on topic TLb3DES external SIGSEGV
Hi,
patch credits is not mine, I've found these in embarcardero forums.

It works but something is wrong with padding and I can not figure out where is it and how i fix this,
As I know, if input data length is up to 8 bytes, then encrypted data length is 8 bytes.
If input data is bigger than 8 bytes, say 9 bytes then output data is 16 bytes and so on. But here, my test program input is 8 bytes
but output is 24 bytes, first thing that I think is that component uses internally 192 bit key...stupid guess maybe.
Or padding is internally solved some other formula.

And I can not use it when data length is wrong.
I make program that interacts with Mifare Ultralight C NFC tag and it's authentication must be done
using 3DES cryptography.

I also attached a modified test program where you can see that encrypted data lenth is 24 butes when input data is only 8 bytes.
and you found modified lbproc.pas file,
function TripleDESEncryptStreamCBC is modified, IV values is replaced with zeroes:
    Block[0] := 0;//DateTimeToFileDate(now);
    Block[1] := 0;//DateTimeToFileDate(now);
    Block[2] := 0;//DateTimeToFileDate(now);
    Block[3] := 0;//DateTimeToFileDate(now);


File Attachment:

File Name: test0.zip
File Size:770 KB

Howto use test program:

GetRandom - returns random plain data

SetKey - Hit it once when you launc a program or when Secret Key is changed
Encrypt - Encrypts data
Decrypt - Decrypts encrypted data
Attachments:
Last edit: 5 years 6 months ago by Ermo.

Please Log in or Create an account to join the conversation.

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Away
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
More
5 years 6 months ago #7132 by Sternas Stefanos
Replied by Sternas Stefanos on topic TLb3DES external SIGSEGV
Give us time Sir
now it's and our lab task..

PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

  • Ermo
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
More
5 years 6 months ago #7135 by Ermo
Replied by Ermo on topic TLb3DES external SIGSEGV
Hi,

Do not search errors or miscalculations. Everything works perfectly.
This object is simply more advanced than I expected.
When I encrypt 8 bytes of data then:

1. object fills Initial Vector IV with random data, encrypts it and stores encryped IV data into first block
2. object encrypts user data, in my case, it is 8 bytes.
3. object calculates data length, encrypts this value and stores it into third block

decryption uses same formula.
1. Object decrypts first block and gets correct IV value
2. object decrypts user data
3. object decrypts data length, checks data integrity and result was plain user data.

So getting three block insead one is solved, and I can say, whole cryptography implementation is correct.

Please Log in or Create an account to join the conversation.

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Away
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
More
5 years 6 months ago #7136 by Sternas Stefanos
Replied by Sternas Stefanos on topic TLb3DES external SIGSEGV
Thanks Sir

PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.