瀏覽代碼

* Remove circular use, add lazarus test project file

(cherry picked from commit ff7febc06cb6146a7f1aa7a94feb1f2056456f8c)
Michaël Van Canneyt 3 年之前
父節點
當前提交
4437bf19e4

+ 8 - 5
packages/fcl-hash/src/fpecc.pp

@@ -1135,13 +1135,16 @@ Var
   i : Integer;
 
 begin
-  P:=@VLI;
-  For I:=0 to SizeOf(TVLI)-1 do
+  if (Pointer(@GetRandomBytes)=Nil) or not GetRandomBytes(@VLI,Sizeof(VLI)) then
     begin
-    P^:=Random(256);
-    Inc(P);
+    P:=@VLI;
+    For I:=0 to SizeOf(TVLI)-1 do
+      begin
+      P^:=Random(256);
+      Inc(P);
+      end;
+    Result:=True;
     end;
-  Result:=True;
 end;
 
 Function EccPublicKeyFromHexa(const Hexa: String) : TEccPublicKey;

+ 43 - 7
packages/fcl-hash/src/fphashutils.pp

@@ -16,7 +16,10 @@ unit fphashutils;
 interface
 
 uses
-  SysUtils, fpECC;
+  SysUtils;
+
+Type
+  EHashUtil = Class(Exception);
 
 Procedure BytesFromVar(out aBytes : TBytes; aLocation : Pointer; aSize : Integer);
 Function BytesFromVar(aLocation : Pointer; aSize : Integer) : TBytes;
@@ -39,6 +42,13 @@ Function BytesEncodeBase64(Source: Tbytes; const IsURL, MultiLines, Padding: Boo
 function CryptoGetRandomBytes(Buffer: PByte; const Count: Integer): Boolean;
 Function ExtractBetween(const ASource,aStart,aEnd : String) : String;
 
+Type
+  TGetRandomBytes = function(aBytes : PByte; aCount: Integer): Boolean;
+
+var
+  GetRandomBytes : TGetRandomBytes;
+
+
 implementation
 
 Procedure BytesFromVar(out aBytes : TBytes; aLocation : Pointer; aSize : Integer);
@@ -319,15 +329,24 @@ type
     function Next: UInt32;
   end;
 
-// TODO: explore Xorshift* instead of CryptoGetRandomNumber
+
 procedure TLecuyer.Seed;
 var
-  VLI: TVLI;
+  VLI: Array[0..2] of byte;
+  I : Integer;
+
 begin
-  EccGetRandomNumber(VLI);
-  rs1 := VLI[0];
-  rs2 := VLI[1];
-  rs3 := VLI[2];
+  I:=0;
+  Repeat
+    Inc(I);
+    if (Pointer(GetRandomBytes)=Nil) or not GetRandomBytes(@VLI,Sizeof(VLI)) then
+       Raise EHashUtil.Create('Cannot seed Lecuyer: no random bytes');
+    rs1 := VLI[0];
+    rs2 := VLI[1];
+    rs3 := VLI[2];
+  Until ((RS1>1) and (rs2>7) and (RS3>15)) or (I>100);
+  if I>100 then
+    Raise EHashUtil.Create('Cannot seed Lecuyer: no suitable random bytes');
   SeedCount := 1;
 end;
 
@@ -384,6 +403,23 @@ begin
 
 end;
 
+function IntGetRandomNumber(aBytes : PByte; aCount: Integer): Boolean;
 
+Var
+  i : Integer;
+  P : PByte;
+
+begin
+  P:=aBytes;
+  For I:=0 to aCount-1 do
+    begin
+    P^:=Random(256);
+    Inc(P);
+    end;
+  Result:=True;
+end;
+
+begin
+  GetRandomBytes:=@IntGetRandomNumber;
 end.
 

+ 102 - 0
packages/fcl-hash/tests/testhash.lpi

@@ -0,0 +1,102 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="12"/>
+    <General>
+      <Flags>
+        <MainUnitHasCreateFormStatements Value="False"/>
+        <MainUnitHasTitleStatement Value="False"/>
+        <MainUnitHasScaledStatement Value="False"/>
+        <UseDefaultCompilerOptions Value="True"/>
+      </Flags>
+      <SessionStorage Value="InProjectDir"/>
+      <Title Value="testhash"/>
+      <UseAppBundle Value="False"/>
+      <ResourceType Value="res"/>
+    </General>
+    <BuildModes>
+      <Item Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+      <UseFileFilters Value="True"/>
+    </PublishOptions>
+    <RunParams>
+      <FormatVersion Value="2"/>
+    </RunParams>
+    <Units>
+      <Unit>
+        <Filename Value="testhash.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="utestsha256.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="../src/fpsha256.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="../src/fphashutils.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="../src/fpecc.pp"/>
+        <IsPartOfProject Value="True"/>
+        <UnitName Value="fpECC"/>
+      </Unit>
+      <Unit>
+        <Filename Value="utestonetimepass.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="utestpem.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="../src/fpasn.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="../src/fpecdsa.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="../src/fppem.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="../src/fpsha512.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="../src/onetimepass.pp"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <Target>
+      <Filename Value="testhash"/>
+    </Target>
+    <SearchPaths>
+      <OtherUnitFiles Value="../src"/>
+      <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
+    </SearchPaths>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions>
+      <Item>
+        <Name Value="EAbort"/>
+      </Item>
+      <Item>
+        <Name Value="ECodetoolError"/>
+      </Item>
+      <Item>
+        <Name Value="EFOpenError"/>
+      </Item>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 1 - 1
packages/fcl-hash/tests/testhash.pp

@@ -5,7 +5,7 @@ program testhash;
 {$mode objfpc}
 
 uses
-  consoletestrunner, utestsha256, utestonetimepass, utestsha512, utestpem;
+  consoletestrunner, utestsha256, utestonetimepass, utestsha512, utestpem, fpECC, fphashutils, fpsha256;
 
 var
   Application: TTestRunner;