Xor-el / CryptoLib4Pascal

Crypto for Modern Object Pascal
MIT License
209 stars 63 forks source link

Access violation in rtl270.bpl #31

Closed Joao-Peterson closed 2 years ago

Joao-Peterson commented 2 years ago

Description Hi, after struggling a little to build the crypto, hash and simplebase libraries i tried to code some AES encryption to see if it works as expected, but while trying i got stuck, the application compiles successfully but an error is raised when i try to execute the first library call, TAesEngine.Create(), that leads me to believe i'm either not compiling my application correctly on Rad studio, some settings might be wrong, or the compiled library has a problem.

To Reproduce Compiled the libraries on Rad Studio 10.4, computer is running Windows 10. Compiled SimpleBaseLib4Pascal, HashLib4Pascal and CryptoLib4Pascal for release, win64 configuration.

Output paths: C:\Users\Public\Documents\Embarcadero\Studio\21.0\Bpl\Win64 C:\Users\Public\Documents\Embarcadero\Studio\21.0\Dcp\Win64

The Bpl\ path is on the system Path env var.

The Dcp's are included on the project via the Project>Options>Runtime Packages menu under All configurations, Link with Runtime Packages is true, Runtime packages contains the packages names. On the little folder icon we can see a dialogue with the option Search path, it also contains both the Dcp and Bpl output paths.

Auto completion can see the symbols on the IDE editor, compiling works nicely, then i make the call:

Crypto.pas:

unit Crypto;

interface

uses
    System.SysUtils,
    ClpAesEngine,
    ClpIAesEngine,
    ClpIBlockCipher,
    ClpIBufferedCipher,
    ClpIBufferedBlockCipher,
    ClpBufferedBlockCipher,
    ClpIKeyParameter,
    ClpParameterUtilities,
    ClpConverters,
    ClpICipherParameters
;

function en(input : String; key : String) : String;

implementation

function en(input : String; key : String) : String;
var
    engine : TAesEngine;
    blockCipher: IBlockCipher;
    cipher: IBufferedCipher;
    keyParameter : IKeyParameter;
    keyBytes : TBytes;
    res : TBytes;
begin
    engine.Create();
    blockCipher := engine as IBlockCipher; // ECB
    cipher := TBufferedBlockCipher.Create(blockCipher) as IBufferedCipher;

    keyBytes := TConverters.ConvertHexStringToBytes(key);
    keyParameter := TParameterUtilities.CreateKeyParameter('AES', keyBytes);

    cipher.Init(true, keyParameter as ICipherParameters);
    res := cipher.DoFinal(TConverters.ConvertHexStringToBytes(input));

    result := TConverters.ConvertBytesToHexString(res, true);
end;

end.

Behavior When i make the call to engine.Create(), first line of the function en(), the programs raises a dialogue of access violation that reads:

Access violation at address 00000000012C7ABE in module 'rtl270.bpl'. Read of address FFFFFFFFFFFFFFFF.

Call stack when trying to call engine.Create():

rtl.System._AfterConstruction($4882A58)
rtl.System.TObject.Create
Cry.en('SYSDBA','773AEF0B12812B2B3A13761CBE167A86')
modal_db.Tmodal_db_form.alterClick(???)
vcl.Vcl.Controls.TControl.Click
vcl.Vcl.StdCtrls.TCustomButton.Click
vcl.Vcl.StdCtrls.TCustomButton.CNCommand(???)
rtl.System.TObject.Dispatch((no value))
vcl.Vcl.Controls.TControl.WndProc($14E3D0)
vcl.Vcl.Controls.TWinControl.WndProc($14E3D0)
vcl.Vcl.StdCtrls.TButtonControl.WndProc($14E3D0)
vcl.Vcl.Controls.TControl.Perform(???,???,???)
vcl.Vcl.Controls.DoControlMsg(???,(no value))
vcl.Vcl.Controls.TWinControl.WMCommand($14E8C0)
vcl.Vcl.Forms.TCustomForm.WMCommand($14E8C0)
rtl.System.TObject.Dispatch((no value))
vcl.Vcl.Controls.TControl.WndProc($14E8C0)
vcl.Vcl.Controls.TWinControl.WndProc($14E8C0)
vcl.Vcl.Forms.TCustomForm.WndProc($14E8C0)
vcl.Vcl.Controls.TWinControl.MainWndProc($14E8C0)
rtl.System.Classes.StdWndProc(???,???,???,???)
:00007FFEDD73E858 ; C:\Windows\System32\user32.dll
:00007FFEDD73DE1B ; C:\Windows\System32\user32.dll
:00007FFEDD73D68A ; C:\Windows\System32\user32.dll
:00007FFEC4FB2467 ; C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.19041.1110_none_60b5254171f9507e\comctl32.dll
:00007FFEC4FC20F0 ; C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.19041.1110_none_60b5254171f9507e\comctl32.dll
:00007FFEDD73E858 ; C:\Windows\System32\user32.dll
:00007FFEDD73E4EE ; C:\Windows\System32\user32.dll
vcl.Vcl.Controls.TWinControl.DefaultHandler((no value))
vcl.Vcl.Controls.TControl.WMLButtonUp($111)
rtl.System.TObject.Dispatch((no value))
vcl.Vcl.Controls.TControl.WndProc($14F2D0)
vcl.Vcl.Controls.TWinControl.WndProc($14F2D0)
vcl.Vcl.StdCtrls.TButtonControl.WndProc($14F2D0)
vcl.Vcl.Controls.TWinControl.MainWndProc($14F2D0)
rtl.System.Classes.StdWndProc(???,???,???,???)
:00007FFEDD73E858 ; C:\Windows\System32\user32.dll
:00007FFEDD73E299 ; C:\Windows\System32\user32.dll
vcl.Vcl.Forms.TApplication.ProcessMessage(nil)
vcl.Vcl.Forms.TApplication.HandleMessage
vcl.Vcl.Forms.TCustomForm.ShowModal
main.Tmain_form.config_dbClick(???)
vcl.Vcl.Menus.TMenuItem.Click
vcl.Vcl.Menus.TMenu.DispatchCommand(???)
vcl.Vcl.Forms.TCustomForm.WMCommand($14FB90)
rtl.System.TObject.Dispatch((no value))
vcl.Vcl.Controls.TControl.WndProc($14FB90)
vcl.Vcl.Controls.TWinControl.WndProc($14FB90)
vcl.Vcl.Forms.TCustomForm.WndProc($14FB90)
vcl.Vcl.Controls.TWinControl.MainWndProc($14FB90)
rtl.System.Classes.StdWndProc(???,???,???,???)
:00007FFEDD73E858 ; C:\Windows\System32\user32.dll
:00007FFEDD73E299 ; C:\Windows\System32\user32.dll
vcl.Vcl.Forms.TApplication.ProcessMessage(nil)
vcl.Vcl.Forms.TApplication.HandleMessage
vcl.Vcl.Forms.TApplication.Run
BloqDeBloq.BloqDeBloq
:00007FFEDEA07034 ; C:\Windows\System32\KERNEL32.DLL
:00007FFEDF602651 ; <UNKNOWN>

Additional context All this is happening on a single form VCL application.

I tried copying the bpl files for the libs side by side with the application executable but it didn't work either.

I'm pretty sure that i missed something while compiling the Libs and or missing something on the application compile/linking as well, and that all this is a miss linked runtime package kinda deal.

Thanks for reading and sorry if i this might be a newbie and/or unrelated question. Best regards.

Xor-el commented 2 years ago

Hello @Joao-Peterson , I deeply apologize as I am just seeing this issue you raised here.

the problem you are encountering is happening at engine.Create();

you are meant to initialize it this way instead

engine := TAesEngine.Create();

apologies once again.

Joao-Peterson commented 2 years ago

Oh my god, sorry about that friend, this is embarrassing. Thanks.