Recent

Author Topic: Error: Project raised exception class 'External:SIGSEGV'.  (Read 225383 times)

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #90 on: December 08, 2020, 08:13:22 pm »
The error happens at this function, this doesnt mean, this function causes it.
Probably this means the problem is detected when the memory where the string is stored is accessed.

SigSEV means: "invalid memory access" and there should be a line number displayed, where the progamm fails.

If there is something wrong with the memory of the string argument, the error should occur when the content of the string is accessed.
So set a breakpoint there and examine the content of the string, or/and insert a Writeln or debugln to see the contents.

Probably the string is trashed by an array overflow or is uninitialized.
If the program is large or recursive, or the string is very large, stack overflow could be a reason.
(I believe there is a write protected guard page at the end of the stack segment, that causes SigSEV at stack overflow, but I do not know it)
Also I recommend to pass the string not by value but by reference as "constref" or "var" parameter to reduce unnecessary stack consumption and for performance.

Actually, the program runs fine.  It gives me this error, but if you try to run it again, it displays the hex strings just as it should.
There is no line number because there is no real error.

Code: [Select]
Uses section:

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, base64, DCPcrypt2, DCPsha256,
  DCPrijndael;

Type section:

type

  { TForm1 }

  TForm1 = class(TForm)
    btnPasswordGen: TButton;
    DCP_rijndael1: TDCP_rijndael;
    DCP_sha256_1: TDCP_sha256;
    lblEncryptedPassword: TLabel;
    lblHash: TLabel;
    memoShaHash: TMemo;
    memoEncryptedPassword: TMemo;
    memoPassword: TMemo;
    procedure btnPasswordGenClick(Sender: TObject); 

Var section:

var
  myHash : string;
  myPass : string;
  myNewPass : string;
  ex : string;
  result : string;
  myStringList: TStringList;
  myStringListEnc: TStringList;
  Cipher : TDCP_rijndael;

  // Variables for encryption
  Key    : AnsiString;
  IV     : AnsiString;
  Data   : AnsiString;

  CBC    : AnsiString;
  ECB    : AnsiString;
  Buffer : AnsiString;

  myKey : AnsiString;
  myIV : AnsiString;
  myData : AnsiString;
  myECB : AnsiString;
  myCBC : AnsiString;
  myBase64ECB : AnsiString;
  myBase64CBC : AnsiString;
  processECB : AnsiString;
  processCBC : AnsiString;

In the Begin/End section:

// Encrypt using Rijndael
   Data := 'Hello World_____';
   Key  := '1234567890______';
   IV   := '______1234567890';

   Cipher := TDCP_rijndael.Create(nil);
   Cipher.Init(Key[1], 256, @IV[1]);
   SetLength(Buffer, Length(Data));
   Cipher.EncryptECB(Data[1], Buffer[1]);
   Cipher.Free;
   ECB := Buffer;

   Cipher := TDCP_rijndael.Create(nil);
   Cipher.Init(Key[1], 256, @IV[1]);
   SetLength(Buffer, Length(Data));
   Cipher.EncryptCBC(Data[1], Buffer[1], Length(Data));
   Cipher.Free;
   CBC := Buffer;       

processECB := BinStr2Hex(ECB);
   processCBC := BinStr2Hex(CBC);

// Now write to the encryption memo control
   // Now write it to the password memo control

   processECB := BinStr2Hex(ECB);
   processCBC := BinStr2Hex(CBC);
   ECB := EncodeStringBase64(ECB);
   CBC := EncodeStringBase64(CBC);

   myKey := 'KEY        : '+ Key + ' - Length: ' + IntToStr(Length(Key));
   myIV := 'IV        : '+ IV + ' - Length: ' + IntToStr(Length(IV));
   myData := 'DATA        : '+ DATA + ' - Length: ' + IntToStr(Length(DATA));
   myECB := 'ECB        : '+ processECB + ' - Length: ' + IntToStr(Length(ECB));
   myCBC := 'CBC        : '+ processCBC + ' - Length: ' + IntToStr(Length(ECB));
   myBase64ECB := 'ECB_BASE64        : '+ ECB + ' - Length: ' + IntToStr(Length(ECB));
   myBase64CBC := 'CBC_BASE64        : '+ CBC + ' - Length: ' + IntToStr(Length(CBC));

   myStringListEnc:=TStringList.Create;               //Create my StringList
   myStringListEnc.Add(myKey);
   myStringListEnc.Add(myIV);
   myStringListEnc.Add(myData);
   myStringListEnc.Add(' ');
   myStringListEnc.Add(myECB);
   myStringListEnc.Add(myCBC);
   myStringListEnc.Add(myBase64ECB);
   myStringListEnc.Add(myBase64CBC);

   memoEncryptedPassword.Lines.Assign(myStringListEnc);               //assign text content
   myStringList.Free;                              //free my StringList

When I press the button to do its thing, I get the error followed by a assembler window with all 000000

Running it again gives me an access violation error, but the routine works and shows in the memo control.

I have no idea what's causing the error.

I am trying to use AES 256 encryption using DCPCrypt2

Thanks for any help.
« Last Edit: December 08, 2020, 08:15:00 pm by karmacomposer »

Peter H

  • Full Member
  • ***
  • Posts: 197
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #91 on: December 08, 2020, 09:12:42 pm »
Just a hint: if you mark code examples as "pascal" in this forum, it becomes better readable and the code window can be enlarged.

I am not the right person to find an error in this code  :-[ it is too advanced for me.
However, I am convinced the error (corrupted data) was caused before calling this routine. (I tried the routine with 4Gb string, no crash, because it was running forever, I had to abort it)
In your projects debug options tick all checkmarks, stack check, range check and so on.
Then have a critical look to the call stack in debugger where you see all previously called routines and you can examine the local variables, even after an unhandled exception.
If you are on a 64 bit machine, you can easily give 1Gb Stack or more to your program, if this makes the error vanish, - or changes it substancially - it is probably a stack overrun.
On 32 bit a little bit less.
(An OS like windows or Linux should only map these memory pages that are actually used, and your program has its own adress space, so this does not mean more memory consumption)
« Last Edit: December 08, 2020, 09:26:26 pm by Peter H »

rvk

  • Hero Member
  • *****
  • Posts: 4479
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #92 on: December 08, 2020, 09:35:12 pm »
Code: [Select]
SetLength(Buffer, Length(Data));
What makes you think that a buffer of size length(data) is enough?
The encrypted output is larger so there is some overwriting of memory space which results in your error.

I can't say exactly how large your buffer needs to be (I'm not behind a computer now) but it should be a multiple of blocksize.


karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #93 on: December 08, 2020, 09:44:29 pm »
Code: [Select]
SetLength(Buffer, Length(Data));
What makes you think that a buffer of size length(data) is enough?
The encrypted output is larger so there is some overwriting of memory space which results in your error.

I can't say exactly how large your buffer needs to be (I'm not behind a computer now) but it should be a multiple of blocksize.

TBH, I am porting my technology from another language to Pascal using Lazarus.  I am very rusty - last time I programmed Pascal was on
Borland Delphi.

How would I enlarge the buffer size? What should I enlarge it to?

Some data to encrypt will be massive, so how would I know how large a buffer size I'd need?

I am using 64 bit Windows 10 and will want to port this to Mac, Linux, etc.

I just added 256 to each buffer size statement just to see if that would even work and I received
the same error, but this time with an address: 10001160F

Does anyone know what this means?  Assembler and machine code is not in my wheelhouse.

Thank you for your help.

Mike
« Last Edit: December 08, 2020, 09:47:15 pm by karmacomposer »

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #94 on: December 08, 2020, 10:04:39 pm »
Ok, doing some experiments, if I change Length(Data) to a whole number, I was able to get to 12 with no errors.

I then wrote this:

Code: [Select]
varDataLen := (Length(Data) div 2);
I made that a int64 in the var staement

The following code works:

Code: [Select]
Cipher := TDCP_rijndael.Create(nil);
   Cipher.Init(Key[1], 256, @IV[1]);
   SetLength(Buffer, varDataLen);
   Cipher.EncryptECB(Data[1], Buffer[1]);
   Cipher.Free;
   ECB := Buffer;

   Cipher := TDCP_rijndael.Create(nil);
   Cipher.Init(Key[1], 256, @IV[1]);
   SetLength(Buffer, varDataLen);
   Cipher.EncryptCBC(Data[1], Buffer[1], varDataLen);
   Cipher.Free;
   CBC := Buffer;             

When I just do the Length of Data it = 24.  So 24 is too large and results in a error.

I am not sure why and what I need, but I need to encrypt this data reliably.

Funny enough, making the length 12 works and results in a ECB and CBC of 16, which is what it needed in this case, however,
other strings may be much larger.  I'll have to mess with it.

Any help is appreciated.

Mike
« Last Edit: December 08, 2020, 10:07:06 pm by karmacomposer »

rvk

  • Hero Member
  • *****
  • Posts: 4479
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #95 on: December 08, 2020, 11:26:40 pm »
Code: [Select]
   Cipher.EncryptCBC(Data[1], Buffer[1], varDataLen);
You still need to pass Length(Data) as third parameter.
Third parameter is the length of data you want to encrypt. Not the size of the buffer.

But looking at your example, your Data is already a multiple of 16 (i.e. 16 exactly). So it should work fine. There would be no need to round the buffer to a 16 block (128 bits) size because it's already at that size. This would be needed if your string is not exactly multiple of 16 characters. See https://stackoverflow.com/a/41290878

But what happens if you use 128 in the Cipher init (like in the original example)?
You passed 256 (why?) which would indicate a key of 32 characters (while your key is just 16 characters).

2nd param to init needs to be keysize in bit (so * 8 ).
Code: Pascal  [Select][+][-]
  1. Cipher := TDCP_rijndael.Create(nil);
  2. Cipher.Init(Key1, Sizeof(Key1) * 8, nil);
  3. Cipher.EncryptECB(InData, Block);
https://wiki.freepascal.org/DCPcrypt
« Last Edit: December 09, 2020, 12:01:35 am by rvk »

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #96 on: December 09, 2020, 03:50:18 pm »
Code: [Select]
   Cipher.EncryptCBC(Data[1], Buffer[1], varDataLen);
You still need to pass Length(Data) as third parameter.
Third parameter is the length of data you want to encrypt. Not the size of the buffer.

But looking at your example, your Data is already a multiple of 16 (i.e. 16 exactly). So it should work fine. There would be no need to round the buffer to a 16 block (128 bits) size because it's already at that size. This would be needed if your string is not exactly multiple of 16 characters. See https://stackoverflow.com/a/41290878

But what happens if you use 128 in the Cipher init (like in the original example)?
You passed 256 (why?) which would indicate a key of 32 characters (while your key is just 16 characters).

2nd param to init needs to be keysize in bit (so * 8 ).
Code: Pascal  [Select][+][-]
  1. Cipher := TDCP_rijndael.Create(nil);
  2. Cipher.Init(Key1, Sizeof(Key1) * 8, nil);
  3. Cipher.EncryptECB(InData, Block);
https://wiki.freepascal.org/DCPcrypt

I need to use AES 256, so how would I achieve that with a 32 character key? I already have the ability to create/trim 32 character keys.

Mike

rvk

  • Hero Member
  • *****
  • Posts: 4479
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #97 on: December 09, 2020, 04:16:40 pm »
I need to use AES 256, so how would I achieve that with a 32 character key? I already have the ability to create/trim 32 character keys.
Make your Key 32 bytes long.

Also... the Data string should be a multiple of 16.
In your case it happens to be that size but if not you can add the following to make sure.
Code: Pascal  [Select][+][-]
  1. if (Length(Data) mod 16 > 0) then
  2.    for I := 1 to (16 - (Length(Data) mod 16)) do
  3.      Data := Data + AnsiChar(0);

If you still get an error you'll need to provide compilable code and input for reproducing this error.

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #98 on: December 09, 2020, 04:57:58 pm »
I need to use AES 256, so how would I achieve that with a 32 character key? I already have the ability to create/trim 32 character keys.
Make your Key 32 bytes long.

Also... the Data string should be a multiple of 16.
In your case it happens to be that size but if not you can add the following to make sure.
Code: Pascal  [Select][+][-]
  1. if (Length(Data) mod 16 > 0) then
  2.    for I := 1 to (16 - (Length(Data) mod 16)) do
  3.      Data := Data + AnsiChar(0);

If you still get an error you'll need to provide compilable code and input for reproducing this error.

Thank you for this.  So, since the data could be any size, from as small as a first name to several paragraphs in size, the above code will simply make sure
it is divisible by 16, correct? As far as making the key 32 bytes long, won't a 32 character key be 32 bytes long?

Thank you very much.  Your help is invaluable.

Mike

rvk

  • Hero Member
  • *****
  • Posts: 4479
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #99 on: December 09, 2020, 05:27:27 pm »
Thank you for this.  So, since the data could be any size, from as small as a first name to several paragraphs in size, the above code will simply make sure
it is divisible by 16, correct?
Yes, the Data should be divisible by Cipher.BlockSize div 8 (which is 128 div 8 = 16).
TDCP_rijndael is inherited from TDCP_blockcipher128 and for that one the standard blocksize is 16, 128 bits.
So you could use Cipher.BlockSize div 8 in the code but because for rijndael it's always 128 bits (16 bytes) you can hardcode the padding to a multiple of 16 characters.

Also... if you look in TDCP_blockcipher128.EncryptCBC() you can see the encoding is done in blocks of 16.
(Set your cursor on .EncryptCBC( and press Alt+Arrow Up and then Shift+Ctrl+Arrow Down. You get right into the source there)

You can see it does blocks of 16 bytes from the for i:= 1 to (Size div 16) and Move(p1^,p2^,16);

As far as making the key 32 bytes long, won't a 32 character key be 32 bytes long?
Yes, a 32 character long ansistring is 32 bytes long. But make sure it's an ansistring and not a utf8 string (which can be longer due to character encoding with special characters).

You could also padd the key if it's shorter than 32 characters. But that might weaken your AES encryption.

Code: Pascal  [Select][+][-]
  1. if (Length(Key) < 32) then
  2.   while Length(Key) < 32 do Key := Key + AnsiChar(0);
  3. Key := Copy(Key, 1, 32); // for the instance Length(Key) > 32

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #100 on: December 09, 2020, 05:55:50 pm »
Thank you for this.  So, since the data could be any size, from as small as a first name to several paragraphs in size, the above code will simply make sure
it is divisible by 16, correct?
Yes, the Data should be divisible by Cipher.BlockSize div 8 (which is 128 div 8 = 16).
TDCP_rijndael is inherited from TDCP_blockcipher128 and for that one the standard blocksize is 16, 128 bits.
So you could use Cipher.BlockSize div 8 in the code but because for rijndael it's always 128 bits (16 bytes) you can hardcode the padding to a multiple of 16 characters.

Also... if you look in TDCP_blockcipher128.EncryptCBC() you can see the encoding is done in blocks of 16.
(Set your cursor on .EncryptCBC( and press Alt+Arrow Up and then Shift+Ctrl+Arrow Down. You get right into the source there)

You can see it does blocks of 16 bytes from the for i:= 1 to (Size div 16) and Move(p1^,p2^,16);

As far as making the key 32 bytes long, won't a 32 character key be 32 bytes long?
Yes, a 32 character long ansistring is 32 bytes long. But make sure it's an ansistring and not a utf8 string (which can be longer due to character encoding with special characters).

You could also padd the key if it's shorter than 32 characters. But that might weaken your AES encryption.

Code: Pascal  [Select][+][-]
  1. if (Length(Key) < 32) then
  2.   while Length(Key) < 32 do Key := Key + AnsiChar(0);
  3. Key := Copy(Key, 1, 32); // for the instance Length(Key) > 32

I get all that, though great info to be sure.  I require AES 256, so is Rijndael AES 128? How would I use it for AES 256? I require very strong encryption.

I have every intention of converting the resulting encrypted AnsiString to UTF8 after and then the reverse when decrypting.
I need to be able to transmit through SSL and I have learned that converting to UTF8 does the trick.

Mike

rvk

  • Hero Member
  • *****
  • Posts: 4479
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #101 on: December 09, 2020, 06:06:39 pm »
I require AES 256, so is Rijndael AES 128? How would I use it for AES 256? I require very strong encryption.
By using a 32 byte key (=256 bits), I thought.
Keysize is different than blocksize.

Quote
For AES, NIST selected three members of the Rijndael family, each with a block size of 128 bits, but three different key lengths: 128, 192 and 256 bits.
https://en.m.wikipedia.org/wiki/Advanced_Encryption_Standard

PascalDragon

  • Hero Member
  • *****
  • Posts: 3187
  • Compiler Developer
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #102 on: December 10, 2020, 09:42:51 am »
As far as making the key 32 bytes long, won't a 32 character key be 32 bytes long?
Yes, a 32 character long ansistring is 32 bytes long. But make sure it's an ansistring and not a utf8 string (which can be longer due to character encoding with special characters).

Though Length will return the length in Byte, not characters or code points, so that wouldn't matter here.

Fred vS

  • Hero Member
  • *****
  • Posts: 2283
    • StrumPract is the musicians best friend
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #103 on: January 12, 2021, 08:14:29 pm »
Hello.

In your code:

Code: Pascal  [Select][+][-]
  1. for I := 0 to High(txtArray) do
  2.     begin
  3.       if txtArray[I] <> '' then
  4.         newArray[I] := txtArray[I]
  5.       else
  6.         newArray[I] := 'empty';
  7.     end;

I dont see before a SetLength(newArray, something);
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

karmacomposer

  • New Member
  • *
  • Posts: 25
Re: Error: Project raised exception class 'External:SIGSEGV'.
« Reply #104 on: January 12, 2021, 08:24:38 pm »
Hello.

In your code:

Code: Pascal  [Select][+][-]
  1. for I := 0 to High(txtArray) do
  2.     begin
  3.       if txtArray[I] <> '' then
  4.         newArray[I] := txtArray[I]
  5.       else
  6.         newArray[I] := 'empty';
  7.     end;

I dont see before a SetLength(newArray, something);

Thank you.

I rewrote the code and found the problems.

Mike

 

TinyPortal © 2005-2018