From ff7febc06cb6146a7f1aa7a94feb1f2056456f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Van=20Canneyt?= Date: Fri, 19 Nov 2021 09:43:21 +0100 Subject: [PATCH] * Remove circular use, add lazarus test project file --- packages/fcl-hash/src/fpecc.pp | 13 ++-- packages/fcl-hash/src/fphashutils.pp | 50 +++++++++++-- packages/fcl-hash/tests/testhash.lpi | 102 +++++++++++++++++++++++++++ packages/fcl-hash/tests/testhash.pp | 2 +- 4 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 packages/fcl-hash/tests/testhash.lpi diff --git a/packages/fcl-hash/src/fpecc.pp b/packages/fcl-hash/src/fpecc.pp index 925b366aab..c4a9593d6a 100644 --- a/packages/fcl-hash/src/fpecc.pp +++ b/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; diff --git a/packages/fcl-hash/src/fphashutils.pp b/packages/fcl-hash/src/fphashutils.pp index f846464702..fd8772e9b0 100644 --- a/packages/fcl-hash/src/fphashutils.pp +++ b/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. diff --git a/packages/fcl-hash/tests/testhash.lpi b/packages/fcl-hash/tests/testhash.lpi new file mode 100644 index 0000000000..9123bfab91 --- /dev/null +++ b/packages/fcl-hash/tests/testhash.lpi @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + <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> diff --git a/packages/fcl-hash/tests/testhash.pp b/packages/fcl-hash/tests/testhash.pp index 0db6a1f7de..5594123a59 100644 --- a/packages/fcl-hash/tests/testhash.pp +++ b/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;