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

View File

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