Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Byte order and TLbBigInt #16

Open
1one-w01f opened this issue Apr 1, 2021 · 2 comments
Open

Byte order and TLbBigInt #16

1one-w01f opened this issue Apr 1, 2021 · 2 comments

Comments

@1one-w01f
Copy link

1one-w01f commented Apr 1, 2021

Hi,

I am trying to use the RSA module to do some signature verification, and I found that the byte order of TLbBigInt to be kind of funny.

If I do this to load pubkey.der which was generated using OpenSSL,

pubKey := TLbRSAKey.Create(aks1024);
pubKey.LoadFromFile('pubkey.der');

then the modular exponentiation seems to be wrong. Somehow I had to reverse the byte order of the modulus to make it working.

However, even if the modular exponentiation works, the signature verification cannot succeed, because at line 688 of LbRSA.pas, the byte orders of SHA1Digest1 and SHA1Digest2 are reverse of each other (I checked the Local Variables by using adding a breakpoint there), and thus Result will be False.

Any thoughts on this?

@1one-w01f
Copy link
Author

If it'd help, here's the source code I used to do the testing. I used Delphi 10.3 Community Edition, and got LockBox installed through the GetIt Package Manager, in case it matters.

program Project2;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, LbRSA, LbAsym, LbBigInt, LbUtils, System.Classes, LbConst;

var
  i: integer;

  pubKey: TLbRSAKey;
  privKey: TLbRSAKey;

  ssa: TLbRSASSA;

  sig: TRSASignatureBlock;
  revsig: TRSASignatureBlock;

  sighexstr: String;

  result: Boolean;

  n: array[0..cBytes1024-1] of Byte = ($bf,$1a,$f5,$1e,$6a,$7b,$5d,$93,$29,$8e,$f9,$e8,$59,$1d,$01,$e4,$c6,$5b,$3f,$60,$00,$16,$00,$73,$fa,$01,$94,$fa,$24,$09,$70,$27,$fc,$5d,$18,$8f,$69,$53,$c3,$77,$47,$c6,$65,$1c,$32,$ca,$f4,$4e,$9f,$54,$97,$06,$fd,$c2,$54,$eb,$0a,$36,$66,$36,$29,$8d,$9d,$b8,$1d,$17,$82,$76,$7b,$07,$db,$87,$a1,$9d,$a9,$1e,$f2,$0e,$d7,$05,$13,$ed,$90,$df,$f1,$a2,$7d,$dc,$02,$0c,$cb,$ce,$b3,$d1,$14,$25,$b7,$43,$a7,$33,$99,
  $6d,$a2,$6d,$6f,$bb,$78,$3b,$e0,$b7,$3f,$a3,$9c,$fe,$eb,$94,$fa,$df,$8c,$35,$36,$44,$b1,$ad,$67,$99,$ce,$9f);
  n_rev: array[0..cBytes1024-1] of Byte;

  e: array[0..0] of Byte = ($03);

begin

  // let's reverse the modulus n
  for i := 0 to cBytes1024-1 do
    n_rev[cBytes1024-1-i] := n[i];

  pubKey := TLbRSAKey.Create(aks1024);
//  pubKey.LoadFromFile('pubkey.der');
  pubKey.Modulus.CopyBuffer(n_rev, cBytes1024);
  pubKey.Exponent.CopyBuffer(e, 1);
  Writeln('pubkey loaded');

  ssa := TlbRSASSA.Create(nil);
  ssa.PublicKey.Assign(pubKey);
  Writeln('pubkey.n = ' + ssa.PublicKey.ModulusAsString);
  Writeln('pubkey.e = ' + ssa.PublicKey.ExponentAsString);

  ssa.HashMethod := hmSHA1;

  sighexstr := '024f9b63004472a035bf6d7dd77247f45b3df6e1d0860ea91000fcba21616bb95a06c128df0c2dda0bec4c7efb559b6cb020e675ef836fb468780905efc8c296' +
'95b03df88d870b84c80e851fcbad53f28c20fda9f1ec8d2a8ffd6648717140fe779c0de58704941fae9f3ac0cf4f6b3f080dd4725ac13fe2d240752f280b671b' ;
  HexToBin(PChar(sighexstr), @sig, length(sig));

  // now let's reverse sig as well ...
  for i := 0 to cBytes1024-1 do
    revsig[cBytes1024-1-i] := sig[i];

  ssa.Signature.CopyBuffer(revsig, length(revsig));


  result := ssa.VerifyString('hello world!');
  Writeln(result);


  Readln(Input);
end.

@romankassebaum
Copy link
Contributor

I have absolutely no idea. It would be nice if you could fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants