fcl-hash: use RDTSCP on i386 and x86-64

This commit is contained in:
mattias 2022-04-29 01:07:59 +02:00
parent 0a44e3192d
commit 6bf0de0ba2
2 changed files with 33 additions and 8 deletions

View File

@ -18,6 +18,11 @@ unit fphashutils;
{$mode ObjFPC}{$H+}
{$modeswitch advancedrecords}
{$IF defined(CPUi386) or defined(CPUx86_64) }
{$define HasRDTSCP}
{$ENDIF}
interface
uses
@ -431,18 +436,39 @@ begin
Result:=Copy(aSource,P1,P2-P1);
end;
{$IFDEF HasRDTSCP}
function RDTSCP: Int64; assembler; nostackframe;
asm
RDTSCP
end;
{$ENDIF}
function IntGetRandomNumber(aBytes : PByte; aCount: Integer): Boolean;
Var
i : Integer;
i: Integer;
P : PByte;
{$IFDEF HasRDTSCP}
{%H-}i64: int64;
j: integer;
{$ENDIF}
begin
P:=aBytes;
For I:=0 to aCount-1 do
i:=0;
while i<aCount do
begin
{$IFDEF HasRDTSCP}
i64:=RDTSCP;
j:=aCount-i;
if j>8 then j:=8;
system.move(i64,P^,j);
inc(p,j);
inc(i,j);
{$ELSE}
P^:=Random(256);
Inc(P);
inc(i);
{$ENDIF}
end;
Result:=True;
end;

View File

@ -16,7 +16,7 @@ Type
protected
procedure GetPEM_PrivateKeyRSA2048bit(out PrivatePEM, PublicPEM: string);
Published
Procedure TestLoad;
Procedure TestECC_Load;
Procedure TestRSA_RS256Verify;
Procedure TestRSA_PrivatePublicPEM_NoPassphrase;
end;
@ -84,7 +84,7 @@ begin
'-----END PUBLIC KEY-----' ]);
end;
procedure TTestPEM.TestLoad;
procedure TTestPEM.TestECC_Load;
Const
// Hex encoded keys, Obtained using XMLRAD
@ -165,19 +165,18 @@ begin
AssertEquals('PublicRSA.ModulusLen=PrivateRSA.ModulusLen',PublicRSA.ModulusLen,PrivateRSA.ModulusLen);
if BICompare(PublicRSA.M,PrivateRSA.M)<>0 then
Fail('PublicRSA.M = PrivateRSA.M');
if BICompare(PublicRSA.E,PrivateRSA.E)<>0 then
Fail('PublicRSA.E = PrivateRSA.E');
Original:=SecretMsg;
// encrypt
// encrypt with public key
SetLength(Encrypted{%H-},PublicRSA.ModulusLen);
EncryptedLen:=RSAEncryptSign(PublicRSA,@Original[1],length(Original),@Encrypted[1],false);
if EncryptedLen<PublicRSA.ModulusLen then
AssertEquals('EncryptedLen = ModulusLen',EncryptedLen,PublicRSA.ModulusLen);
// decrypt
// decrypt with private key
SetLength(Decrypted{%H-},EncryptedLen);
DecryptedLen:=RSADecryptVerify(PrivateRSA,@Encrypted[1],@Decrypted[1],EncryptedLen,false);