Recent

Author Topic: Delphi Encryption Compendium ported to Free Pascal  (Read 9148 times)

Starchild

  • New Member
  • *
  • Posts: 13
Delphi Encryption Compendium ported to Free Pascal
« on: November 20, 2017, 01:03:56 am »
Hello,

I'm currently porting a Delphi project to FPC which is using the Delphi Encryption Compendium library. DEC is a powerful crypto library originally written by Hagen Reddmann. Since it wasn't available for FPC and required more than a few syntax tweaks to work on other platforms, I decided to port it myself.
Check it out on Github for more details: https://github.com/decfpc/DelphiEncryptionCompendium

I know there are many alternatives, but I like this one because of its versatility and cleanness. Encrypting, hashing and formatting is all done in the same manner and all algorithms are cleanly encapsulated in objects (no HashSHA1(), Base64() functions etc. flying around in the namespace).

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #1 on: November 20, 2017, 02:13:54 am »
Well done. But it looks like it only works with FPC 3.1.1 i.e FPC Trunk

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

Starchild

  • New Member
  • *
  • Posts: 13
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #2 on: November 20, 2017, 03:47:51 am »
Well done. But it looks like it only works with FPC 3.1.1 i.e FPC Trunk

JD

Have you tried it with an older version, if so what didn't work? As I wrote in the readme, 2.6.4 worked for me. Only one minor change was required in DECUtil.RandomSystemTime(), I commented the line there. FPC 2.6 didn't have GetTickCount64 and I wasn't sure how to add a compile-time check that's compatible with Delphi. Writing {$IF (fpc_version >= 3)} would break Delphi. And {$IFDEF VER3} will be incorrect once FPC v4 comes out.

edwinyzh

  • New Member
  • *
  • Posts: 43
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #3 on: November 22, 2017, 04:45:30 am »
Good job and it's great that you preserve the Delphi support!

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #4 on: November 22, 2017, 05:12:51 am »
Only one minor change was required in DECUtil.RandomSystemTime(), I commented the line there. FPC 2.6 didn't have GetTickCount64 and I wasn't sure how to add a compile-time check that's compatible with Delphi.

something along the lines of

Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}
  2.   {$IF (fpc_version < 3) and defined(Linux)}
  3.     function GetTickCount64: QWord;
  4.     var
  5.       tp: timespec;
  6.     begin
  7.       clock_gettime(CLOCK_MONOTONIC, @tp); // exists since Linux Kernel 2.6
  8.       Result := (Int64(tp.tv_sec) * 1000) + (tp.tv_nsec div 1000000);
  9.     end;
  10.   {$ENDIF}
  11. {$ENDIF}
  12.  

PS:
Gettickcount64 shamelessly copied from the unit lazutf8sysutils of lazarus 1.4.4 so I know it works. I added the Defined(linux) because as far as I can see on that unit in windows you use the performance counters instead of GetTickCount. The code above needs a bit of finesse(ing) to make it a bit more global (bsd, macos etc) but it should not give any hiccups to delphi.
« Last Edit: November 22, 2017, 05:32:06 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

harros

  • Newbie
  • Posts: 4
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #5 on: June 01, 2018, 01:10:20 pm »
Can someone please shed some light on deploying this library in a Lazarus/FPC project? I tried searching online, there is not much info on this library in general. When I gave it a try by adding the .pas file to the project via "Project Inspector", and hit "Compile" (Ctrl + F9), the compiler fails with the following message:

zstream.pp(28,23) Fatal: Cannot find unit gzio used by zstream. Make sure all ppu files of a package are in its output directory. ppu in wrong directory=C:\lazarus144\fpc\2.6.4\units\i386-win32\paszlib\gzio.ppu..

Note: I have the DEC library (with the folder name of dec) sitting in the project folder. Please refer to the attachment for the compiler path settings. I am using Lazarus version 1.4.4 with FPC version 2.6.4 in Windows 7 SP1 x86.
« Last Edit: June 01, 2018, 01:13:10 pm by harros »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #6 on: June 01, 2018, 01:49:31 pm »
To select current trunk and higher do a

Code: Pascal  [Select][+][-]
  1. {$if FPC_FULLVERSION>30100}
  2.   trunk and higher
  3. {$else}
  4.   lower
  5. {$endif}

Similarly, 3.0.4 is 30004 etc. Note that afaik 3.0.4 does have gettickcount64.

Note that FPC comes with both an CPU and CRC unit, so better find different names for those units.
« Last Edit: June 01, 2018, 01:55:03 pm by marcov »

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #7 on: June 01, 2018, 03:04:08 pm »
Can someone please shed some light on deploying this library in a Lazarus/FPC project? I tried searching online, there is not much info on this library in general. When I gave it a try by adding the .pas file to the project via "Project Inspector", and hit "Compile" (Ctrl + F9), the compiler fails with the following message:

zstream.pp(28,23) Fatal: Cannot find unit gzio used by zstream. Make sure all ppu files of a package are in its output directory. ppu in wrong directory=C:\lazarus144\fpc\2.6.4\units\i386-win32\paszlib\gzio.ppu..

Note: I have the DEC library (with the folder name of dec) sitting in the project folder. Please refer to the attachment for the compiler path settings. I am using Lazarus version 1.4.4 with FPC version 2.6.4 in Windows 7 SP1 x86.

This library will only work for FPC 3.1.1 trunk and above.
What exactly do you want to do with the library?
May be we can offer similar alternatives.

harros

  • Newbie
  • Posts: 4
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #8 on: June 01, 2018, 03:47:53 pm »
To select current trunk and higher do a

Code: Pascal  [Select][+][-]
  1. {$if FPC_FULLVERSION>30100}
  2.   trunk and higher
  3. {$else}
  4.   lower
  5. {$endif}

Similarly, 3.0.4 is 30004 etc. Note that afaik 3.0.4 does have gettickcount64.

Note that FPC comes with both an CPU and CRC unit, so better find different names for those units.

Thanks for the advice, marcov. You are right, the error is actually due to badly named CRC unit in the library, the evil twin is forcing FPC to recompile the units which utilize CRC unit in FPC library. Given that gettickcount64 function is available in both FPC v2.6.4 and v3.0.4, I will leave the code intact.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #9 on: June 01, 2018, 03:52:18 pm »
What this has missing is a test suite against e.g. OpenSSL and LibreSSL and CryptLib to confirm the validity against the standards.
Last time I looked at DEC half of it failed such tests.  But OK I will have a new look....
Specialize a type, not a var.

harros

  • Newbie
  • Posts: 4
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #10 on: June 01, 2018, 04:15:03 pm »
This library will only work for FPC 3.1.1 trunk and above.
What exactly do you want to do with the library?
May be we can offer similar alternatives.

Thanks for offering help, I am trying to match RC2 and Rijndael (with block size of 256 bit) cipher outputs from a MS .NET application. I did look into other alternatives before coming across DEC:

1. DCPcrypt - RC2 cipher output never matches the output from .NET application, and I decided to give up DCPcrypt after several attempts. Somemore,  the Rijndael block size is fixed to 128 bit (AES standard block size).
2. Synopse mORMot - the framework does not offer RC2 encryption.
3. Fundamentals 5 - the library does offer RC2 encryption, but the block size of Rijndael algorithm is fixed to 128 bit.
« Last Edit: June 01, 2018, 04:30:22 pm by harros »

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #11 on: June 01, 2018, 04:26:07 pm »
Thanks for offering help, I am trying to match RC2 and AES (with block size = 256 bit) cipher outputs from a MS .NET application.
There is no AES with block size = 256 bit! AES always has 128-bit blocks. Do you mean key size 256-bit or Rijndael with 256 bit blocks?

harros

  • Newbie
  • Posts: 4
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #12 on: June 01, 2018, 04:39:22 pm »
There is no AES with block size = 256 bit! AES always has 128-bit blocks. Do you mean key size 256-bit or Rijndael with 256 bit blocks?

Sorry for confusion, I should say Rijndael with 256 bit block size.

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #13 on: June 01, 2018, 07:24:40 pm »

Thanks for offering help, I am trying to match RC2 and Rijndael (with block size of 256 bit) cipher outputs from a MS .NET application. I did look into other alternatives before coming across DEC:

1. DCPcrypt - RC2 cipher output never matches the output from .NET application, and I decided to give up DCPcrypt after several attempts. Somemore,  the Rijndael block size is fixed to 128 bit (AES standard block size).
2. Synopse mORMot - the framework does not offer RC2 encryption.
3. Fundamentals 5 - the library does offer RC2 encryption, but the block size of Rijndael algorithm is fixed to 128 bit.

Have you seen/used http://wiki.freepascal.org/HashLib4Pascal   ?

-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: Delphi Encryption Compendium ported to Free Pascal
« Reply #14 on: June 01, 2018, 07:29:42 pm »
Have you seen/used http://wiki.freepascal.org/HashLib4Pascal   ?
Totally irrelevant. This lib does not contain any encryption algorithms.

 

TinyPortal © 2005-2018