Recent

Author Topic: Blowfish help  (Read 12814 times)

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Blowfish help
« on: January 05, 2013, 05:44:22 am »
below is the code which is producing error, but i am unable to understand that actually what is the problem, plz someone help.

Code: [Select]
Type
  PBLOWFISH_CTX = ^BLOWFISH_CTX;
  BLOWFISH_CTX = record
    P:array [0..18] of LongInt;
    S:array [0..4,0..256] of LongInt;
  End;

Procedure Blowfish_Init(ctx : PBLOWFISH_CTX; Key : PByteArray; KeyLen : Integer); cdecl; external;

.....

Var
  ctx : PBLOWFISH_CTX;
  S : String;
  Key : TByteArray;

Begin
    S := 'TESTKEY'; 
    FillChar(Key,SizeOf(Key),0);
    Move(S[1],Key,Min(SizeOf(Key),Length(Key)));

=> Error : Blowfish_Init(ctx,@Key,Length(S)); 


Assembler Code

Code: [Select]
Blowfish_Init
00423220 55                       push   %ebp
00423221 57                       push   %edi
00423222 56                       push   %esi
00423223 53                       push   %ebx
00423224 83ec24                   sub    $0x24,%esp
00423227 8b742438                 mov    0x38(%esp),%esi
0042322B 8b6c243c                 mov    0x3c(%esp),%ebp
0042322F 31c9                     xor    %ecx,%ecx
00423231 31d2                     xor    %edx,%edx
00423233 8d3c31                   lea    (%ecx,%esi,1),%edi
00423236 6690                     xchg   %ax,%ax
00423238 8b9c9100b04200           mov    0x42b000(%ecx,%edx,4),%ebx
=>0042323F 895c9748                 mov    %ebx,0x48(%edi,%edx,4)
00423243 42                       inc    %edx
00423244 81fa00010000             cmp    $0x100,%edx
0042324A 75ec                     jne    0x423238 <Blowfish_Init+24>

Holiday season is online now. :-)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Blowfish help
« Reply #1 on: January 05, 2013, 09:13:01 am »
Why not use the native FCL Blowfish implementation (fpc/packages/fcl-base/src/blowfish.pp) rather than an external routine?
Then it would be easier to debug, and this community would be more familiar with it.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Blowfish help
« Reply #2 on: January 05, 2013, 09:58:53 am »
Quote
Procedure Blowfish_Init(ctx : PBLOWFISH_CTX; Key : PByteArray; KeyLen : Integer); cdecl; external;

.....

Var
  ctx : PBLOWFISH_CTX;
  S : String;
  Key : TByteArray;
Are you sure this code even compile?

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Blowfish help
« Reply #3 on: January 05, 2013, 10:20:22 am »
Why not use the native FCL Blowfish implementation (fpc/packages/fcl-base/src/blowfish.pp) rather than an external routine?
Then it would be easier to debug, and this community would be more familiar with it.


I would love to use it, but i cannot, i have to use this external library only. If i had to do in 'c', then it is simple, but pascal is new to me. Till the date when i had started lazarus, i had faced small challenges, which i had successfully completed with the help of this forum. This is only time i had got stuck,
Holiday season is online now. :-)

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Blowfish help
« Reply #4 on: January 05, 2013, 10:23:14 am »
Quote
Procedure Blowfish_Init(ctx : PBLOWFISH_CTX; Key : PByteArray; KeyLen : Integer); cdecl; external;

.....

Var
  ctx : PBLOWFISH_CTX;
  S : String;
  Key : TByteArray;
Are you sure this code even compile?


sir this is the part of code. Program successfully compile  and run till Blowfish_Init encounters..
Holiday season is online now. :-)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Blowfish help
« Reply #5 on: January 05, 2013, 12:54:36 pm »
Quote
sir this is the part of code. Program successfully compile  and run till Blowfish_Init encounters..
Oops, sorry. I didn't see that the Blowfish_Init is an external declaration.

What is TByteArray? If it's dynamic array then it might be the problem. Another possibility is the calling convention of Blowfish_Init. Ensure it's really cdecl and not stdcall (or even something else though it's very unlikely).

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Blowfish help
« Reply #6 on: January 05, 2013, 01:09:15 pm »
Quote
sir this is the part of code. Program successfully compile  and run till Blowfish_Init encounters..
Oops, sorry. I didn't see that the Blowfish_Init is an external declaration.

What is TByteArray? If it's dynamic array then it might be the problem. Another possibility is the calling convention of Blowfish_Init. Ensure it's really cdecl and not stdcall (or even something else though it's very unlikely).



This is C Header File Declaration
Code: [Select]

typedef struct {
  unsigned long P[16 + 2];
  unsigned long S[4][256];
} BLOWFISH_CTX;

void Blowfish_Init(BLOWFISH_CTX *ctx, unsigned char *key, int keyLen);
void Blowfish_Encrypt(BLOWFISH_CTX *ctx, unsigned long *xl, unsigned long *xr);
void Blowfish_Decrypt(BLOWFISH_CTX *ctx, unsigned long *xl, unsigned long *xr);


and i had converted it as follows

Code: [Select]
Unit unit1;

{$mode objfpc}
{$LinkLib 'ex_bf_0001.a'} //blowfish lib

Interface

Uses
  Classes, Sysutils;

Type
 // PBLOWFISH_CTX = ^BLOWFISH_CTX;
  BLOWFISH_CTX = record
    P:array [0..18] of LongInt;
    S:array [0..4, 0..256] of LongInt;
  End;


Type TByteArr = Array [0..32] of Byte;
Type PByteArr = ^TByteArr;

Procedure Blowfish_Init(ctx : BLOWFISH_CTX; Key : TByteArr; KeyLen : Integer); cdecl; external;
Procedure Blowfish_Encrypt(ctx: BLOWFISH_CTX; xl: LongInt; xr: LongInt);cdecl;external;
Procedure Blowfish_Decrypt(ctx: BLOWFISH_CTX; xl: LongInt; xr: LongInt);cdecl;external;


I know, i am messing some important thing, but i am unable to figure it out.
Holiday season is online now. :-)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Blowfish help
« Reply #7 on: January 05, 2013, 01:22:05 pm »
This
Code: [Select]
BLOWFISH_CTX = record
    P:array [0..18] of LongInt;
    S:array [0..4, 0..256] of LongInt;
  End;
is not a correct translation of this
Code: [Select]
typedef struct {
  unsigned long P[16 + 2];
  unsigned long S[4][256];
} BLOWFISH_CTX;
C arrays are 0 based, and the one in the square bracket is the LENGTH, not UPPER BOUND. Therefore [16 + 2] translates to [0 .. 17], [4][256] translates to [0 .. 3][0 .. 255].

unsigned long converts to LongWord. It's better to use ctype unit in order to have type compatibility with C. You can use culong for unsigned long, cint for int, etc.

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Blowfish help
« Reply #8 on: January 05, 2013, 01:53:25 pm »
This
Code: [Select]
BLOWFISH_CTX = record
    P:array [0..18] of LongInt;
    S:array [0..4, 0..256] of LongInt;
  End;
is not a correct translation of this
Code: [Select]
typedef struct {
  unsigned long P[16 + 2];
  unsigned long S[4][256];
} BLOWFISH_CTX;
C arrays are 0 based, and the one in the square bracket is the LENGTH, not UPPER BOUND. Therefore [16 + 2] translates to [0 .. 17], [4][256] translates to [0 .. 3][0 .. 255].

unsigned long converts to LongWord. It's better to use ctype unit in order to have type compatibility with C. You can use culong for unsigned long, cint for int, etc.


I had done as you said, but the error continues to be the same... :(
Holiday season is online now. :-)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Blowfish help
« Reply #9 on: January 05, 2013, 02:55:48 pm »
I can't help any further without having the dll... well, assuming your translation is correct and you do everything as expected in the library documentation.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Blowfish help
« Reply #10 on: January 05, 2013, 03:53:37 pm »
Type TByteArr = Array [0..32] of Byte;
Type PByteArr = ^TByteArr;


Use PByte type with Lazarus. It works the way you assume PByteArr would. In Delphi, the same used to be PByteArray.

Code: [Select]
Blowfish_Init(ctx,@Key,Length(S));Pointer to pointer on Key, and ctx is uninitialized. I think it should be
Code: [Select]
Var
  ctx : BLOWFISH_CTX; // without P*
...
Blowfish_Init(@ctx, Key, Length(S));
Also, don't you need to put something in ctx record first? (I don't know)
« Last Edit: January 05, 2013, 03:56:24 pm by User137 »

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Blowfish help
« Reply #11 on: January 08, 2013, 05:14:29 am »
I can't help any further without having the dll... well, assuming your translation is correct and you do everything as expected in the library documentation.


 
Code: [Select]
var
 Key : AnsiString;
begin
  Key := 'TESTKEY';

  uBlowFish.Blowfish_Init(@ctx,@Key[1],Length(Key));



hi, i had done this according to your valuable guidance, but i got struct is something very new to me. that is

Quote

To encrypt a 64-bit block, call Blowfish_Encrypt with a pointer to
       BLOWFISH_CTX, a pointer to the 32-bit left half of the plaintext
      and a pointer to the 32-bit right half.  The plaintext will be
      overwritten with the ciphertext.

Procedure Blowfish_Encrypt(ctx: PBLOWFISH_CTX; xl: PLongWord; xr: PLongWord);cdecl;external;

Here x1 : 32 bit and Xr : 32 bit.

I am consfused how to convert a value into two 32 bit value (xl.xr) and vice versa for this operation.


I had found the same thing in fcl-blowfish implemention.
Code: [Select]
TBFBlock     = array[0..1] of LongInt;     { BlowFish }
But how to convert a TStream to TBFBlock, i got nothing in return. please help..
« Last Edit: January 08, 2013, 05:25:25 am by deepaak99 »
Holiday season is online now. :-)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Blowfish help
« Reply #12 on: January 08, 2013, 07:34:13 am »
Quote
I am consfused how to convert a value into two 32 bit value (xl.xr) and vice versa for this operation.
I'm quite confused as well since xl and xr is a pointer to 32-bit value (instead of PChar), in what encoding should the plaintext be? Nevertheless, assuming the plaintext is really stored in a 32-bit array, you can split to xl and xr with (say the whole plaintext is kept in variable pt):
Code: [Select]
xl := @pt[0];
xr := @pt[Length[pt] div 2];
Quote
But how to convert a TStream to TBFBlock
You can't, both are different thing. Unless you specify what "convert" will do to the TStream such that it can produce a TBFBlock.

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Blowfish help
« Reply #13 on: January 08, 2013, 11:48:30 am »
Quote
I am consfused how to convert a value into two 32 bit value (xl.xr) and vice versa for this operation.
I'm quite confused as well since xl and xr is a pointer to 32-bit value (instead of PChar), in what encoding should the plaintext be? Nevertheless, assuming the plaintext is really stored in a 32-bit array, you can split to xl and xr with (say the whole plaintext is kept in variable pt):
Code: [Select]
xl := @pt[0];
xr := @pt[Length[pt] div 2];
Quote
But how to convert a TStream to TBFBlock
You can't, both are different thing. Unless you specify what "convert" will do to the TStream such that it can produce a TBFBlock.

here  64bit(qword) is to splitted into two 32bit(LongWord).

I found many example but cannot understand upto mark, or maybe i am not satisfied whether it is the correct way or not.
does fpc/lazarus has some inbuilt function to split 64bit value into two 32 bit (longword) values. If yes, then it will be good as gold.  ;D

Holiday season is online now. :-)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Blowfish help
« Reply #14 on: January 08, 2013, 11:57:45 am »
There is propably some shl/shr bitshift way, but you can also use record.
Code: [Select]
TInt64Parts = record
  a, b: dword;
end;
...
var i64: int64; lowint: dword;
begin
  lowint:=TInt64Parts(i64).a;
Hope it works.

 

TinyPortal © 2005-2018