Skip to content

Buffer overflow in EncryptHash/DecryptHash (Move of 64 bytes into 16-byte TAESState) #115

Open

Description

@fsansei

*In MEncryptDecryptAES.pas, EncryptHash and DecryptHash do:

SetLength(InputBytes, 64);
FillChar(State, SizeOf(State), 0);
Move(InputBytes[0], State, Length(InputBytes));

TAESState is array[0..3, 0..3] of Byte (16 bytes), so this writes 48 bytes
past the end of State. It is undefined behavior: it may appear to work on
Windows, but on Android (ARM, FMX) it overwrote other locals (in my case
InputBytes itself ended up with Length = 0) and the output differed from
Windows.

The same pattern exists in EncryptPassword (Move with Length of the UTF-8
password bytes) and DecryptPassword (Move with Length of the decoded hex),
which overflow for inputs longer than 16 bytes.

Fix (EncryptHash):
SetLength(InputBytes, SizeOf(State)); // pads with zeros up to 16
Move(InputBytes[0], State, SizeOf(State));

Fix (DecryptHash): validate Length(InputBytes) = SizeOf(State) and raise an
error otherwise, then Move(InputBytes[0], State, SizeOf(State)).

Note: data encrypted with the current version may not decrypt after the fix,
since the overflow can corrupt ExpandedKey depending on the compiler's
stack layout.

Activity

  1. fsansei commented on Oct 23, 2026

    @fsansei
    Author

    “Sorry, wrong repository”.

  2. MHumm commented on Oct 24, 2026

    @MHumm
    Owner

    Glad you founr that it's not our library. Which one do you use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions