Home > Net >  Different results on Windows and Linux for ElAES.pas
Different results on Windows and Linux for ElAES.pas

Time:04-17

I'm trying compile my project for Linux, have such code in ElAES.pas:

type
  TAESKey256 = array [0..31] of byte;
  TAESExpandedKey256 = array [0..63] of longword;
  PLongWord = ^LongWord;

procedure ExpandAESKeyForEncryption(const Key: TAESKey256; var ExpandedKey: TAESExpandedKey256); overload;
begin
  ExpandedKey[0] := PLongWord(@Key[0])^;
  ExpandedKey[1] := PLongWord(@Key[4])^;
  ExpandedKey[2] := PLongWord(@Key[8])^;
  ExpandedKey[3] := PLongWord(@Key[12])^;
  ExpandedKey[4] := PLongWord(@Key[16])^;
  ExpandedKey[5] := PLongWord(@Key[20])^;
  ExpandedKey[6] := PLongWord(@Key[24])^;
  ExpandedKey[7] := PLongWord(@Key[28])^;

And I have one result in ExpandedKey on Windows (32/64bit) and different result on Linux and MacOS. Key value is the same on both platforms. What I should change to get the same result on Linux/MAC like on Windows?

CodePudding user response:

Usually, if the code works correctly in Win32, I test it in Win64. This helps catch errors related to different lengths of some types for 32 and 64 bits. This has always worked for me, but only until this case. The above code worked correctly in Win64. But there were errors when using in MacOS. The last thing I thought about was the difference in type sizes. And as it turned out - in vain. LongWord turned out to be a very strange type, the size of which is the same for Windows 32/64 bits and larger for Linux/MacOS. After changing the types in the module, everything worked on all platforms with any bit depth.

type
  TAESKey256 = array [0..31] of byte;
  TAESExpandedKey256 = array [0..63] of UInt32;
  PUInt32 = ^UInt32;

procedure ExpandAESKeyForEncryption(const Key: TAESKey256; var ExpandedKey: TAESExpandedKey256); overload;
begin
  ExpandedKey[0] := PUInt32(@Key[0])^;
  ExpandedKey[1] := PUInt32(@Key[4])^;
  ExpandedKey[2] := PUInt32(@Key[8])^;
  ExpandedKey[3] := PUInt32(@Key[12])^;
  • Related