Browse Source

update HashLib to be in sync with latest version

Ugochukwu Mmaduekwe 6 years ago
parent
commit
2ac9523ccd

+ 8 - 12
src/libraries/hashlib4pascal/HlpBlake2B.pas

@@ -77,6 +77,7 @@ type
 {$ENDIF USE_UNROLLED_VARIANT}
 {$ENDIF USE_UNROLLED_VARIANT}
     F_bufferFilled, FHashSize, FBlockSize: Int32;
     F_bufferFilled, FHashSize, FBlockSize: Int32;
     F_counter0, F_counter1, F_finalizationFlag0, F_finalizationFlag1: UInt64;
     F_counter0, F_counter1, F_finalizationFlag0, F_finalizationFlag1: UInt64;
+    FtreeConfig: IBlake2BTreeConfig;
 
 
     class constructor Blake2BConfig();
     class constructor Blake2BConfig();
 
 
@@ -1634,6 +1635,7 @@ var
 begin
 begin
 
 
   Lconfig := config;
   Lconfig := config;
+  FtreeConfig := treeConfig;
   FBlockSize := BlockSizeInBytes;
   FBlockSize := BlockSizeInBytes;
 
 
   if (Lconfig = Nil) then
   if (Lconfig = Nil) then
@@ -1641,7 +1643,7 @@ begin
     Lconfig := FDefaultConfig;
     Lconfig := FDefaultConfig;
   end;
   end;
 
 
-  FrawConfig := TBlake2BIvBuilder.ConfigB(Lconfig, treeConfig);
+  FrawConfig := TBlake2BIvBuilder.ConfigB(Lconfig, FtreeConfig);
   if ((Lconfig.Key <> Nil) and (System.Length(Lconfig.Key) <> 0)) then
   if ((Lconfig.Key <> Nil) and (System.Length(Lconfig.Key) <> 0)) then
   begin
   begin
 
 
@@ -1670,6 +1672,11 @@ begin
 
 
   F_finalizationFlag0 := System.High(UInt64);
   F_finalizationFlag0 := System.High(UInt64);
 
 
+  if (FtreeConfig.IsLastNode) then
+  begin
+    F_finalizationFlag1 := System.High(UInt64);
+  end;
+
   count := System.Length(F_buf) - F_bufferFilled;
   count := System.Length(F_buf) - F_bufferFilled;
 
 
   if count > 0 then
   if count > 0 then
@@ -1725,9 +1732,7 @@ begin
 
 
   if (FKey <> Nil) then
   if (FKey <> Nil) then
   begin
   begin
-
     TransformBytes(FKey, 0, System.Length(FKey));
     TransformBytes(FKey, 0, System.Length(FKey));
-
   end;
   end;
 
 
 end;
 end;
@@ -1773,11 +1778,8 @@ begin
 
 
   if (a_data_length > 0) then
   if (a_data_length > 0) then
   begin
   begin
-
     System.Move(a_data[offset], F_buf[F_bufferFilled], a_data_length);
     System.Move(a_data[offset], F_buf[F_bufferFilled], a_data_length);
-
     F_bufferFilled := F_bufferFilled + a_data_length;
     F_bufferFilled := F_bufferFilled + a_data_length;
-
   end;
   end;
 end;
 end;
 
 
@@ -1785,18 +1787,12 @@ function TBlake2B.TransformFinal: IHashResult;
 var
 var
   tempRes: THashLibByteArray;
   tempRes: THashLibByteArray;
 begin
 begin
-
   Finish();
   Finish();
-
   System.SetLength(tempRes, FHashSize);
   System.SetLength(tempRes, FHashSize);
-
   TConverters.le64_copy(PUInt64(Fm_state), 0, PByte(tempRes), 0,
   TConverters.le64_copy(PUInt64(Fm_state), 0, PByte(tempRes), 0,
     System.Length(tempRes));
     System.Length(tempRes));
-
   Result := THashResult.Create(tempRes);
   Result := THashResult.Create(tempRes);
-
   Initialize();
   Initialize();
-
 end;
 end;
 
 
 function TBlake2B.GetName: String;
 function TBlake2B.GetName: String;

+ 81 - 61
src/libraries/hashlib4pascal/HlpBlake2BIvBuilder.pas

@@ -18,6 +18,8 @@ resourcestring
   SInvalidPersonalisationLength =
   SInvalidPersonalisationLength =
     '"Personalisation" Length Must Be Equal To 16';
     '"Personalisation" Length Must Be Equal To 16';
   SInvalidSaltLength = '"Salt" Length Must Be Equal To 16';
   SInvalidSaltLength = '"Salt" Length Must Be Equal To 16';
+  STreeIncorrectInnerHashSize =
+    'Tree Inner Hash Size Must Not Be Greater Than 64';
 
 
 type
 type
   TBlake2BIvBuilder = class sealed(TObject)
   TBlake2BIvBuilder = class sealed(TObject)
@@ -27,14 +29,14 @@ type
 
 
       FSequentialTreeConfig: IBlake2BTreeConfig;
       FSequentialTreeConfig: IBlake2BTreeConfig;
 
 
+    class procedure VerifyConfigB(const config: IBlake2BConfig;
+      const treeConfig: IBlake2BTreeConfig; isSequential: Boolean); static;
+
     class constructor Blake2BIvBuilder();
     class constructor Blake2BIvBuilder();
 
 
   public
   public
     class function ConfigB(const config: IBlake2BConfig;
     class function ConfigB(const config: IBlake2BConfig;
-      const treeConfig: IBlake2BTreeConfig): THashLibUInt64Array; static;
-
-    class procedure ConfigBSetNode(const rawConfig: THashLibUInt64Array;
-      depth: Byte; nodeOffset: UInt64); static; inline;
+      var treeConfig: IBlake2BTreeConfig): THashLibUInt64Array; static;
 
 
   end;
   end;
 
 
@@ -42,29 +44,9 @@ implementation
 
 
 { TBlake2BIvBuilder }
 { TBlake2BIvBuilder }
 
 
-class constructor TBlake2BIvBuilder.Blake2BIvBuilder;
-begin
-  FSequentialTreeConfig := TBlake2BTreeConfig.Create();
-  FSequentialTreeConfig.IntermediateHashSize := 0;
-  FSequentialTreeConfig.LeafSize := 0;
-  FSequentialTreeConfig.FanOut := 1;
-  FSequentialTreeConfig.MaxHeight := 1;
-end;
-
-class function TBlake2BIvBuilder.ConfigB(const config: IBlake2BConfig;
-  const treeConfig: IBlake2BTreeConfig): THashLibUInt64Array;
-var
-  isSequential: Boolean;
-  LtreeConfig: IBlake2BTreeConfig;
-  rawConfig: THashLibUInt64Array;
+class procedure TBlake2BIvBuilder.VerifyConfigB(const config: IBlake2BConfig;
+  const treeConfig: IBlake2BTreeConfig; isSequential: Boolean);
 begin
 begin
-  LtreeConfig := treeConfig;
-  isSequential := LtreeConfig = Nil;
-  if (isSequential) then
-  begin
-    LtreeConfig := FSequentialTreeConfig;
-  end;
-  System.SetLength(rawConfig, 8);
 
 
   // digest length
   // digest length
   if ((config.HashSize <= 0) or (config.HashSize > 64)) then
   if ((config.HashSize <= 0) or (config.HashSize > 64)) then
@@ -72,8 +54,6 @@ begin
     raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidHashSize);
     raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidHashSize);
   end;
   end;
 
 
-  rawConfig[0] := rawConfig[0] or (UInt64(UInt32(config.HashSize)));
-
   // Key length
   // Key length
   if (config.Key <> Nil) then
   if (config.Key <> Nil) then
   begin
   begin
@@ -81,40 +61,18 @@ begin
     begin
     begin
       raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidKeyLength);
       raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidKeyLength);
     end;
     end;
-    rawConfig[0] := rawConfig[0] or
-      UInt64(UInt32(System.Length(config.Key)) shl 8);
   end;
   end;
 
 
-  // FanOut
-  rawConfig[0] := rawConfig[0] or (UInt32(LtreeConfig.FanOut) shl 16);
-  // Depth
-  rawConfig[0] := rawConfig[0] or (UInt32(LtreeConfig.MaxHeight) shl 24);
-  // Leaf length
-  rawConfig[0] := rawConfig[0] or
-    ((UInt64(UInt32(LtreeConfig.LeafSize))) shl 32);
-
-  // Inner length
-  if ((not isSequential) and ((LtreeConfig.IntermediateHashSize <= 0) or
-    (LtreeConfig.IntermediateHashSize > 64))) then
-  begin
-    raise EArgumentOutOfRangeHashLibException.Create
-      ('treeConfig.TreeIntermediateHashSize');
-  end;
-  rawConfig[2] := rawConfig[2] or
-    (UInt32(LtreeConfig.IntermediateHashSize) shl 8);
-
-  // Salt
+  // Salt length
   if (config.Salt <> Nil) then
   if (config.Salt <> Nil) then
   begin
   begin
     if (System.Length(config.Salt) <> 16) then
     if (System.Length(config.Salt) <> 16) then
     begin
     begin
       raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidSaltLength);
       raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidSaltLength);
     end;
     end;
-    rawConfig[4] := TConverters.ReadBytesAsUInt64LE(PByte(config.Salt), 0);
-    rawConfig[5] := TConverters.ReadBytesAsUInt64LE(PByte(config.Salt), 8);
   end;
   end;
 
 
-  // Personalisation
+  // Personalisation length
   if (config.Personalisation <> Nil) then
   if (config.Personalisation <> Nil) then
   begin
   begin
     if (System.Length(config.Personalisation) <> 16) then
     if (System.Length(config.Personalisation) <> 16) then
@@ -122,20 +80,82 @@ begin
       raise EArgumentOutOfRangeHashLibException.CreateRes
       raise EArgumentOutOfRangeHashLibException.CreateRes
         (@SInvalidPersonalisationLength);
         (@SInvalidPersonalisationLength);
     end;
     end;
-    rawConfig[6] := TConverters.ReadBytesAsUInt64LE
-      (PByte(config.Personalisation), 0);
-    rawConfig[7] := TConverters.ReadBytesAsUInt64LE
-      (PByte(config.Personalisation), 8);
   end;
   end;
 
 
-  result := rawConfig;
+  // Tree InnerHashSize
+  if (treeConfig <> Nil) then
+  begin
+
+    if ((not isSequential) and ((treeConfig.InnerHashSize <= 0))) then
+    begin
+      raise EArgumentOutOfRangeHashLibException.Create
+        ('treeConfig.TreeIntermediateHashSize');
+    end;
+
+    if (treeConfig.InnerHashSize > 64) then
+    begin
+      raise EArgumentOutOfRangeHashLibException.CreateRes
+        (@STreeIncorrectInnerHashSize);
+    end;
+  end;
+
+end;
+
+class constructor TBlake2BIvBuilder.Blake2BIvBuilder;
+begin
+  FSequentialTreeConfig := TBlake2BTreeConfig.Create();
+  FSequentialTreeConfig.FanOut := 1;
+  FSequentialTreeConfig.MaxDepth := 1;
+  FSequentialTreeConfig.LeafSize := 0;
+  FSequentialTreeConfig.NodeOffset := 0;
+  FSequentialTreeConfig.NodeDepth := 0;
+  FSequentialTreeConfig.InnerHashSize := 0;
+  FSequentialTreeConfig.IsLastNode := False;
 end;
 end;
 
 
-class procedure TBlake2BIvBuilder.ConfigBSetNode(const rawConfig
-  : THashLibUInt64Array; depth: Byte; nodeOffset: UInt64);
+class function TBlake2BIvBuilder.ConfigB(const config: IBlake2BConfig;
+  var treeConfig: IBlake2BTreeConfig): THashLibUInt64Array;
+var
+  isSequential: Boolean;
+  tempBuffer: THashLibByteArray;
 begin
 begin
-  rawConfig[1] := nodeOffset;
-  rawConfig[2] := (rawConfig[2] and (not UInt64($FF))) or depth;
+  isSequential := treeConfig = Nil;
+  if (isSequential) then
+  begin
+    treeConfig := FSequentialTreeConfig;
+  end;
+
+  VerifyConfigB(config, treeConfig, isSequential);
+
+  System.SetLength(tempBuffer, 64);
+
+  tempBuffer[0] := config.HashSize;
+  tempBuffer[1] := System.Length(config.Key);
+
+  if treeConfig <> Nil then
+  begin
+    tempBuffer[2] := treeConfig.FanOut;
+    tempBuffer[3] := treeConfig.MaxDepth;
+    TConverters.ReadUInt32AsBytesLE(treeConfig.LeafSize, tempBuffer, 4);
+    TConverters.ReadUInt64AsBytesLE(treeConfig.NodeOffset, tempBuffer, 8);
+    tempBuffer[16] := treeConfig.NodeDepth;
+    tempBuffer[17] := treeConfig.InnerHashSize;
+  end;
+
+  if config.Salt <> Nil then
+  begin
+    System.Move(config.Salt[0], tempBuffer[32], 16 * System.SizeOf(Byte));
+  end;
+
+  if config.Personalisation <> Nil then
+  begin
+    System.Move(config.Personalisation[0], tempBuffer[48],
+      16 * System.SizeOf(Byte));
+  end;
+
+  System.SetLength(Result, 8);
+  TConverters.le64_copy(PByte(tempBuffer), 0, PUInt64(Result), 0,
+    System.Length(tempBuffer) * System.SizeOf(Byte));
 end;
 end;
 
 
 end.
 end.

+ 133 - 37
src/libraries/hashlib4pascal/HlpBlake2BTreeConfig.pas

@@ -5,7 +5,18 @@ unit HlpBlake2BTreeConfig;
 interface
 interface
 
 
 uses
 uses
-  HlpIBlake2BTreeConfig;
+  HlpIBlake2BTreeConfig,
+  HlpHashLibTypes;
+
+resourcestring
+  SInvalidFanOutParameter =
+    'FanOut Value Should be Between [0 .. 255] for Blake2B';
+  SInvalidMaxDepthParameter =
+    'FanOut Value Should be Between [1 .. 255] for Blake2B';
+  SInvalidNodeDepthParameter =
+    'NodeDepth Value Should be Between [0 .. 255] for Blake2B';
+  SInvalidInnerHashSizeParameter =
+    'InnerHashSize Value Should be Between [0 .. 64] for Blake2B';
 
 
 type
 type
 
 
@@ -13,29 +24,53 @@ type
 
 
   strict private
   strict private
 
 
-    FIntermediateHashSize, FMaxHeight, FFanOut: Int32;
-    FLeafSize: Int64;
+    FFanOut, FMaxDepth, FNodeDepth, FInnerHashSize: Byte;
+    FLeafSize: UInt32;
+    FNodeOffset: UInt64;
+    FIsLastNode: Boolean;
 
 
-    function GetIntermediateHashSize: Int32; inline;
-    procedure SetIntermediateHashSize(value: Int32); inline;
-    function GetMaxHeight: Int32; inline;
-    procedure SetMaxHeight(value: Int32); inline;
-    function GetLeafSize: Int64; inline;
-    procedure SetLeafSize(value: Int64); inline;
-    function GetFanOut: Int32; inline;
-    procedure SetFanOut(value: Int32); inline;
+    procedure ValidateFanOut(AFanOut: Byte); inline;
+    procedure ValidateInnerHashSize(AInnerHashSize: Byte); inline;
+    procedure ValidateMaxDepth(AMaxDepth: Byte); inline;
+    procedure ValidateNodeDepth(ANodeDepth: Byte); inline;
+
+    function GetFanOut: Byte; inline;
+    procedure SetFanOut(value: Byte); inline;
+
+    function GetMaxDepth: Byte; inline;
+    procedure SetMaxDepth(value: Byte); inline;
+
+    function GetNodeDepth: Byte; inline;
+    procedure SetNodeDepth(value: Byte); inline;
+
+    function GetInnerHashSize: Byte; inline;
+    procedure SetInnerHashSize(value: Byte); inline;
+
+    function GetLeafSize: UInt32; inline;
+    procedure SetLeafSize(value: UInt32); inline;
+
+    function GetNodeOffset: UInt64; inline;
+    procedure SetNodeOffset(value: UInt64); inline;
+
+    function GetIsLastNode: Boolean; inline;
+    procedure SetIsLastNode(value: Boolean); inline;
 
 
   public
   public
     constructor Create();
     constructor Create();
-    property IntermediateHashSize: Int32 read GetIntermediateHashSize
-      write SetIntermediateHashSize;
-    property MaxHeight: Int32 read GetMaxHeight write SetMaxHeight;
 
 
-    property LeafSize: Int64 read GetLeafSize write SetLeafSize;
-    property FanOut: Int32 read GetFanOut write SetFanOut;
+    property FanOut: Byte read GetFanOut write SetFanOut;
+
+    property MaxDepth: Byte read GetMaxDepth write SetMaxDepth;
+
+    property NodeDepth: Byte read GetNodeDepth write SetNodeDepth;
+
+    property InnerHashSize: Byte read GetInnerHashSize write SetInnerHashSize;
+
+    property LeafSize: UInt32 read GetLeafSize write SetLeafSize;
 
 
-    class function CreateInterleaved(parallelism: Int32)
-      : IBlake2BTreeConfig; static;
+    property NodeOffset: UInt64 read GetNodeOffset write SetNodeOffset;
+
+    property IsLastNode: Boolean read GetIsLastNode write SetIsLastNode;
 
 
   end;
   end;
 
 
@@ -43,59 +78,120 @@ implementation
 
 
 { TBlake2BTreeConfig }
 { TBlake2BTreeConfig }
 
 
-class function TBlake2BTreeConfig.CreateInterleaved(parallelism: Int32)
-  : IBlake2BTreeConfig;
+procedure TBlake2BTreeConfig.ValidateFanOut(AFanOut: Byte);
+begin
+  if not(AFanOut in [0 .. 255]) then
+  begin
+    raise EArgumentInvalidHashLibException.CreateRes(@SInvalidFanOutParameter);
+  end;
+end;
+
+procedure TBlake2BTreeConfig.ValidateInnerHashSize(AInnerHashSize: Byte);
 begin
 begin
-  result := TBlake2BTreeConfig.Create();
-  result.FanOut := parallelism;
-  result.MaxHeight := 2;
-  result.IntermediateHashSize := 64;
+  if not(AInnerHashSize in [0 .. 64]) then
+  begin
+    raise EArgumentInvalidHashLibException.CreateRes
+      (@SInvalidInnerHashSizeParameter);
+  end;
 end;
 end;
 
 
-function TBlake2BTreeConfig.GetFanOut: Int32;
+procedure TBlake2BTreeConfig.ValidateMaxDepth(AMaxDepth: Byte);
+begin
+  if not(AMaxDepth in [1 .. 255]) then
+  begin
+    raise EArgumentInvalidHashLibException.CreateRes
+      (@SInvalidMaxDepthParameter);
+  end;
+end;
+
+procedure TBlake2BTreeConfig.ValidateNodeDepth(ANodeDepth: Byte);
+begin
+  if not(ANodeDepth in [0 .. 255]) then
+  begin
+    raise EArgumentInvalidHashLibException.CreateRes
+      (@SInvalidNodeDepthParameter);
+  end;
+end;
+
+function TBlake2BTreeConfig.GetFanOut: Byte;
 begin
 begin
   result := FFanOut;
   result := FFanOut;
 end;
 end;
 
 
-function TBlake2BTreeConfig.GetIntermediateHashSize: Int32;
+function TBlake2BTreeConfig.GetInnerHashSize: Byte;
+begin
+  result := FInnerHashSize;
+end;
+
+function TBlake2BTreeConfig.GetIsLastNode: Boolean;
 begin
 begin
-  result := FIntermediateHashSize;
+  result := FIsLastNode;
 end;
 end;
 
 
-function TBlake2BTreeConfig.GetLeafSize: Int64;
+function TBlake2BTreeConfig.GetLeafSize: UInt32;
 begin
 begin
   result := FLeafSize;
   result := FLeafSize;
 end;
 end;
 
 
-function TBlake2BTreeConfig.GetMaxHeight: Int32;
+function TBlake2BTreeConfig.GetMaxDepth: Byte;
 begin
 begin
-  result := FMaxHeight;
+  result := FMaxDepth;
 end;
 end;
 
 
-procedure TBlake2BTreeConfig.SetFanOut(value: Int32);
+function TBlake2BTreeConfig.GetNodeDepth: Byte;
 begin
 begin
+  result := FNodeDepth;
+end;
+
+function TBlake2BTreeConfig.GetNodeOffset: UInt64;
+begin
+  result := FNodeOffset;
+end;
+
+procedure TBlake2BTreeConfig.SetFanOut(value: Byte);
+begin
+  ValidateFanOut(value);
   FFanOut := value;
   FFanOut := value;
 end;
 end;
 
 
-procedure TBlake2BTreeConfig.SetIntermediateHashSize(value: Int32);
+procedure TBlake2BTreeConfig.SetInnerHashSize(value: Byte);
 begin
 begin
-  FIntermediateHashSize := value;
+  ValidateInnerHashSize(value);
+  FInnerHashSize := value;
 end;
 end;
 
 
-procedure TBlake2BTreeConfig.SetLeafSize(value: Int64);
+procedure TBlake2BTreeConfig.SetIsLastNode(value: Boolean);
+begin
+  FIsLastNode := value;
+end;
+
+procedure TBlake2BTreeConfig.SetLeafSize(value: UInt32);
 begin
 begin
   FLeafSize := value;
   FLeafSize := value;
 end;
 end;
 
 
-procedure TBlake2BTreeConfig.SetMaxHeight(value: Int32);
+procedure TBlake2BTreeConfig.SetMaxDepth(value: Byte);
+begin
+  ValidateMaxDepth(value);
+  FMaxDepth := value;
+end;
+
+procedure TBlake2BTreeConfig.SetNodeDepth(value: Byte);
+begin
+  ValidateNodeDepth(value);
+  FNodeDepth := value;
+end;
+
+procedure TBlake2BTreeConfig.SetNodeOffset(value: UInt64);
 begin
 begin
-  FMaxHeight := value;
+  FNodeOffset := value;
 end;
 end;
 
 
 constructor TBlake2BTreeConfig.Create;
 constructor TBlake2BTreeConfig.Create;
 begin
 begin
   Inherited Create();
   Inherited Create();
-  FIntermediateHashSize := 64;
+  ValidateInnerHashSize(64);
+  FInnerHashSize := 64;
 end;
 end;
 
 
 end.
 end.

+ 8 - 12
src/libraries/hashlib4pascal/HlpBlake2S.pas

@@ -77,6 +77,7 @@ type
 {$ENDIF USE_UNROLLED_VARIANT}
 {$ENDIF USE_UNROLLED_VARIANT}
     F_bufferFilled, FHashSize, FBlockSize: Int32;
     F_bufferFilled, FHashSize, FBlockSize: Int32;
     F_counter0, F_counter1, F_finalizationFlag0, F_finalizationFlag1: UInt32;
     F_counter0, F_counter1, F_finalizationFlag0, F_finalizationFlag1: UInt32;
+    FtreeConfig: IBlake2STreeConfig;
 
 
     class constructor Blake2SConfig();
     class constructor Blake2SConfig();
 
 
@@ -1415,6 +1416,7 @@ var
 begin
 begin
 
 
   Lconfig := config;
   Lconfig := config;
+  FtreeConfig := treeConfig;
   FBlockSize := BlockSizeInBytes;
   FBlockSize := BlockSizeInBytes;
 
 
   if (Lconfig = Nil) then
   if (Lconfig = Nil) then
@@ -1422,7 +1424,7 @@ begin
     Lconfig := FDefaultConfig;
     Lconfig := FDefaultConfig;
   end;
   end;
 
 
-  FrawConfig := TBlake2SIvBuilder.ConfigS(Lconfig, treeConfig);
+  FrawConfig := TBlake2SIvBuilder.ConfigS(Lconfig, FtreeConfig);
   if ((Lconfig.Key <> Nil) and (System.Length(Lconfig.Key) <> 0)) then
   if ((Lconfig.Key <> Nil) and (System.Length(Lconfig.Key) <> 0)) then
   begin
   begin
 
 
@@ -1451,6 +1453,11 @@ begin
 
 
   F_finalizationFlag0 := System.High(UInt32);
   F_finalizationFlag0 := System.High(UInt32);
 
 
+  if (FtreeConfig.IsLastNode) then
+  begin
+    F_finalizationFlag1 := System.High(UInt32);
+  end;
+
   count := System.Length(F_buf) - F_bufferFilled;
   count := System.Length(F_buf) - F_bufferFilled;
 
 
   if count > 0 then
   if count > 0 then
@@ -1506,9 +1513,7 @@ begin
 
 
   if (FKey <> Nil) then
   if (FKey <> Nil) then
   begin
   begin
-
     TransformBytes(FKey, 0, System.Length(FKey));
     TransformBytes(FKey, 0, System.Length(FKey));
-
   end;
   end;
 
 
 end;
 end;
@@ -1554,11 +1559,8 @@ begin
 
 
   if (a_data_length > 0) then
   if (a_data_length > 0) then
   begin
   begin
-
     System.Move(a_data[offset], F_buf[F_bufferFilled], a_data_length);
     System.Move(a_data[offset], F_buf[F_bufferFilled], a_data_length);
-
     F_bufferFilled := F_bufferFilled + a_data_length;
     F_bufferFilled := F_bufferFilled + a_data_length;
-
   end;
   end;
 end;
 end;
 
 
@@ -1566,18 +1568,12 @@ function TBlake2S.TransformFinal: IHashResult;
 var
 var
   tempRes: THashLibByteArray;
   tempRes: THashLibByteArray;
 begin
 begin
-
   Finish();
   Finish();
-
   System.SetLength(tempRes, FHashSize);
   System.SetLength(tempRes, FHashSize);
-
   TConverters.le32_copy(PCardinal(Fm_state), 0, PByte(tempRes), 0,
   TConverters.le32_copy(PCardinal(Fm_state), 0, PByte(tempRes), 0,
     System.Length(tempRes));
     System.Length(tempRes));
-
   Result := THashResult.Create(tempRes);
   Result := THashResult.Create(tempRes);
-
   Initialize();
   Initialize();
-
 end;
 end;
 
 
 function TBlake2S.GetName: String;
 function TBlake2S.GetName: String;

+ 88 - 62
src/libraries/hashlib4pascal/HlpBlake2SIvBuilder.pas

@@ -17,6 +17,8 @@ resourcestring
   SInvalidKeyLength = '"Key" Length Must Not Be Greater Than 32';
   SInvalidKeyLength = '"Key" Length Must Not Be Greater Than 32';
   SInvalidPersonalisationLength = '"Personalisation" Length Must Be Equal To 8';
   SInvalidPersonalisationLength = '"Personalisation" Length Must Be Equal To 8';
   SInvalidSaltLength = '"Salt" Length Must Be Equal To 8';
   SInvalidSaltLength = '"Salt" Length Must Be Equal To 8';
+  STreeIncorrectInnerHashSize =
+    'Tree Inner Hash Size Must Not Be Greater Than 32';
 
 
 type
 type
   TBlake2SIvBuilder = class sealed(TObject)
   TBlake2SIvBuilder = class sealed(TObject)
@@ -26,14 +28,14 @@ type
 
 
       FSequentialTreeConfig: IBlake2STreeConfig;
       FSequentialTreeConfig: IBlake2STreeConfig;
 
 
+    class procedure VerifyConfigS(const config: IBlake2SConfig;
+      const treeConfig: IBlake2STreeConfig; isSequential: Boolean); static;
+
     class constructor Blake2SIvBuilder();
     class constructor Blake2SIvBuilder();
 
 
   public
   public
     class function ConfigS(const config: IBlake2SConfig;
     class function ConfigS(const config: IBlake2SConfig;
-      const treeConfig: IBlake2STreeConfig): THashLibUInt32Array; static;
-
-    // class procedure ConfigSSetNode(const rawConfig: THashLibUInt32Array; depth: Byte;
-    // nodeOffset: UInt32); static; inline;
+      var treeConfig: IBlake2STreeConfig): THashLibUInt32Array; static;
 
 
   end;
   end;
 
 
@@ -41,29 +43,9 @@ implementation
 
 
 { TBlake2SIvBuilder }
 { TBlake2SIvBuilder }
 
 
-class constructor TBlake2SIvBuilder.Blake2SIvBuilder;
+class procedure TBlake2SIvBuilder.VerifyConfigS(const config: IBlake2SConfig;
+  const treeConfig: IBlake2STreeConfig; isSequential: Boolean);
 begin
 begin
-  FSequentialTreeConfig := TBlake2STreeConfig.Create();
-  FSequentialTreeConfig.IntermediateHashSize := 0;
-  FSequentialTreeConfig.LeafSize := 0;
-  FSequentialTreeConfig.FanOut := 1;
-  FSequentialTreeConfig.MaxHeight := 1;
-end;
-
-class function TBlake2SIvBuilder.ConfigS(const config: IBlake2SConfig;
-  const treeConfig: IBlake2STreeConfig): THashLibUInt32Array;
-var
-  isSequential: Boolean;
-  rawConfig: THashLibUInt32Array;
-  LtreeConfig: IBlake2STreeConfig;
-begin
-  LtreeConfig := treeConfig;
-  isSequential := LtreeConfig = Nil;
-  if (isSequential) then
-  begin
-    LtreeConfig := FSequentialTreeConfig;
-  end;
-  System.SetLength(rawConfig, 8);
 
 
   // digest length
   // digest length
   if ((config.HashSize <= 0) or (config.HashSize > 32)) then
   if ((config.HashSize <= 0) or (config.HashSize > 32)) then
@@ -71,8 +53,6 @@ begin
     raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidHashSize);
     raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidHashSize);
   end;
   end;
 
 
-  rawConfig[0] := rawConfig[0] or (UInt32(config.HashSize));
-
   // Key length
   // Key length
   if (config.Key <> Nil) then
   if (config.Key <> Nil) then
   begin
   begin
@@ -80,39 +60,18 @@ begin
     begin
     begin
       raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidKeyLength);
       raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidKeyLength);
     end;
     end;
-    rawConfig[0] := rawConfig[0] or UInt32(System.Length(config.Key)) shl 8;
-  end;
-
-  // FanOut
-  rawConfig[0] := rawConfig[0] or (UInt32(LtreeConfig.FanOut) shl 16);
-  // Depth
-  rawConfig[0] := rawConfig[0] or (UInt32(LtreeConfig.MaxHeight) shl 24);
-  // Leaf length
-  rawConfig[0] := rawConfig[0] or
-    ((UInt64(UInt32(LtreeConfig.LeafSize))) shl 32);
-
-  // Inner length
-  if ((not isSequential) and ((LtreeConfig.IntermediateHashSize <= 0) or
-    (LtreeConfig.IntermediateHashSize > 32))) then
-  begin
-    raise EArgumentOutOfRangeHashLibException.Create
-      ('treeConfig.TreeIntermediateHashSize');
   end;
   end;
-  rawConfig[2] := rawConfig[2] or
-    (UInt32(LtreeConfig.IntermediateHashSize) shl 8);
 
 
-  // Salt
+  // Salt length
   if (config.Salt <> Nil) then
   if (config.Salt <> Nil) then
   begin
   begin
     if (System.Length(config.Salt) <> 8) then
     if (System.Length(config.Salt) <> 8) then
     begin
     begin
       raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidSaltLength);
       raise EArgumentOutOfRangeHashLibException.CreateRes(@SInvalidSaltLength);
     end;
     end;
-    rawConfig[4] := TConverters.ReadBytesAsUInt32LE(PByte(config.Salt), 0);
-    rawConfig[5] := TConverters.ReadBytesAsUInt32LE(PByte(config.Salt), 4);
   end;
   end;
 
 
-  // Personalisation
+  // Personalisation length
   if (config.Personalisation <> Nil) then
   if (config.Personalisation <> Nil) then
   begin
   begin
     if (System.Length(config.Personalisation) <> 8) then
     if (System.Length(config.Personalisation) <> 8) then
@@ -120,20 +79,87 @@ begin
       raise EArgumentOutOfRangeHashLibException.CreateRes
       raise EArgumentOutOfRangeHashLibException.CreateRes
         (@SInvalidPersonalisationLength);
         (@SInvalidPersonalisationLength);
     end;
     end;
-    rawConfig[6] := TConverters.ReadBytesAsUInt32LE
-      (PByte(config.Personalisation), 0);
-    rawConfig[7] := TConverters.ReadBytesAsUInt32LE
-      (PByte(config.Personalisation), 4);
   end;
   end;
 
 
-  result := rawConfig;
+  // Tree InnerHashSize
+  if (treeConfig <> Nil) then
+  begin
+
+    if ((not isSequential) and ((treeConfig.InnerHashSize <= 0))) then
+    begin
+      raise EArgumentOutOfRangeHashLibException.Create
+        ('treeConfig.TreeIntermediateHashSize');
+    end;
+
+    if (treeConfig.InnerHashSize > 32) then
+    begin
+      raise EArgumentOutOfRangeHashLibException.CreateRes
+        (@STreeIncorrectInnerHashSize);
+    end;
+  end;
+
 end;
 end;
 
 
-// class procedure TBlake2SIvBuilder.ConfigSSetNode(const rawConfig: THashLibUInt32Array;
-// depth: Byte; nodeOffset: UInt32);
-// begin
-// rawConfig[1] := nodeOffset;
-// rawConfig[2] := (rawConfig[2] and (not UInt32($FF))) or depth;
-// end;
+class constructor TBlake2SIvBuilder.Blake2SIvBuilder;
+begin
+  FSequentialTreeConfig := TBlake2STreeConfig.Create();
+  FSequentialTreeConfig.FanOut := 1;
+  FSequentialTreeConfig.MaxDepth := 1;
+  FSequentialTreeConfig.LeafSize := 0;
+  FSequentialTreeConfig.NodeOffset := 0;
+  FSequentialTreeConfig.NodeDepth := 0;
+  FSequentialTreeConfig.InnerHashSize := 0;
+  FSequentialTreeConfig.IsLastNode := False;
+end;
+
+class function TBlake2SIvBuilder.ConfigS(const config: IBlake2SConfig;
+  var treeConfig: IBlake2STreeConfig): THashLibUInt32Array;
+var
+  isSequential: Boolean;
+  tempBuffer: THashLibByteArray;
+begin
+  isSequential := treeConfig = Nil;
+  if (isSequential) then
+  begin
+    treeConfig := FSequentialTreeConfig;
+  end;
+
+  VerifyConfigS(config, treeConfig, isSequential);
+
+  System.SetLength(tempBuffer, 32);
+
+  tempBuffer[0] := config.HashSize;
+  tempBuffer[1] := System.Length(config.Key);
+
+  if treeConfig <> Nil then
+  begin
+    tempBuffer[2] := treeConfig.FanOut;
+    tempBuffer[3] := treeConfig.MaxDepth;
+    TConverters.ReadUInt32AsBytesLE(treeConfig.LeafSize, tempBuffer, 4);
+    tempBuffer[8] := Byte(treeConfig.NodeOffset);
+    tempBuffer[9] := Byte(treeConfig.NodeOffset shr 8);
+    tempBuffer[10] := Byte(treeConfig.NodeOffset shr 16);
+    tempBuffer[11] := Byte(treeConfig.NodeOffset shr 24);
+    tempBuffer[12] := Byte(treeConfig.NodeOffset shr 32);
+    tempBuffer[13] := Byte(treeConfig.NodeOffset shr 40);
+    tempBuffer[14] := treeConfig.NodeDepth;
+    tempBuffer[15] := treeConfig.InnerHashSize;
+  end;
+
+  if config.Salt <> Nil then
+  begin
+    System.Move(config.Salt[0], tempBuffer[16], 8 * System.SizeOf(Byte));
+  end;
+
+  if config.Personalisation <> Nil then
+  begin
+    System.Move(config.Personalisation[0], tempBuffer[24],
+      8 * System.SizeOf(Byte));
+  end;
+
+  System.SetLength(Result, 8);
+  TConverters.le32_copy(PByte(tempBuffer), 0, PCardinal(Result), 0,
+    System.Length(tempBuffer) * System.SizeOf(Byte));
+end;
 
 
 end.
 end.

+ 146 - 37
src/libraries/hashlib4pascal/HlpBlake2STreeConfig.pas

@@ -5,7 +5,20 @@ unit HlpBlake2STreeConfig;
 interface
 interface
 
 
 uses
 uses
-  HlpIBlake2STreeConfig;
+  HlpIBlake2STreeConfig,
+  HlpHashLibTypes;
+
+resourcestring
+  SInvalidFanOutParameter =
+    'FanOut Value Should be Between [0 .. 255] for Blake2S';
+  SInvalidMaxDepthParameter =
+    'FanOut Value Should be Between [1 .. 255] for Blake2S';
+  SInvalidNodeDepthParameter =
+    'NodeDepth Value Should be Between [0 .. 255] for Blake2S';
+  SInvalidInnerHashSizeParameter =
+    'InnerHashSize Value Should be Between [0 .. 32] for Blake2S';
+  SInvalidNodeOffsetParameter =
+    'NodeOffset Value Should be Between [0 .. (2^48-1)] for Blake2S';
 
 
 type
 type
 
 
@@ -13,29 +26,54 @@ type
 
 
   strict private
   strict private
 
 
-    FIntermediateHashSize, FMaxHeight, FFanOut: Int32;
-    FLeafSize: Int64;
+    FFanOut, FMaxDepth, FNodeDepth, FInnerHashSize: Byte;
+    FLeafSize: UInt32;
+    FNodeOffset: UInt64;
+    FIsLastNode: Boolean;
 
 
-    function GetIntermediateHashSize: Int32; inline;
-    procedure SetIntermediateHashSize(value: Int32); inline;
-    function GetMaxHeight: Int32; inline;
-    procedure SetMaxHeight(value: Int32); inline;
-    function GetLeafSize: Int64; inline;
-    procedure SetLeafSize(value: Int64); inline;
-    function GetFanOut: Int32; inline;
-    procedure SetFanOut(value: Int32); inline;
+    procedure ValidateFanOut(AFanOut: Byte); inline;
+    procedure ValidateInnerHashSize(AInnerHashSize: Byte); inline;
+    procedure ValidateMaxDepth(AMaxDepth: Byte); inline;
+    procedure ValidateNodeDepth(ANodeDepth: Byte); inline;
+    procedure ValidateNodeOffset(ANodeOffset: UInt64); inline;
+
+    function GetFanOut: Byte; inline;
+    procedure SetFanOut(value: Byte); inline;
+
+    function GetMaxDepth: Byte; inline;
+    procedure SetMaxDepth(value: Byte); inline;
+
+    function GetNodeDepth: Byte; inline;
+    procedure SetNodeDepth(value: Byte); inline;
+
+    function GetInnerHashSize: Byte; inline;
+    procedure SetInnerHashSize(value: Byte); inline;
+
+    function GetLeafSize: UInt32; inline;
+    procedure SetLeafSize(value: UInt32); inline;
+
+    function GetNodeOffset: UInt64; inline;
+    procedure SetNodeOffset(value: UInt64); inline;
+
+    function GetIsLastNode: Boolean; inline;
+    procedure SetIsLastNode(value: Boolean); inline;
 
 
   public
   public
     constructor Create();
     constructor Create();
-    property IntermediateHashSize: Int32 read GetIntermediateHashSize
-      write SetIntermediateHashSize;
-    property MaxHeight: Int32 read GetMaxHeight write SetMaxHeight;
 
 
-    property LeafSize: Int64 read GetLeafSize write SetLeafSize;
-    property FanOut: Int32 read GetFanOut write SetFanOut;
+    property FanOut: Byte read GetFanOut write SetFanOut;
+
+    property MaxDepth: Byte read GetMaxDepth write SetMaxDepth;
+
+    property NodeDepth: Byte read GetNodeDepth write SetNodeDepth;
+
+    property InnerHashSize: Byte read GetInnerHashSize write SetInnerHashSize;
+
+    property LeafSize: UInt32 read GetLeafSize write SetLeafSize;
+
+    property NodeOffset: UInt64 read GetNodeOffset write SetNodeOffset;
 
 
-    class function CreateInterleaved(parallelism: Int32)
-      : IBlake2STreeConfig; static;
+    property IsLastNode: Boolean read GetIsLastNode write SetIsLastNode;
 
 
   end;
   end;
 
 
@@ -43,59 +81,130 @@ implementation
 
 
 { TBlake2STreeConfig }
 { TBlake2STreeConfig }
 
 
-class function TBlake2STreeConfig.CreateInterleaved(parallelism: Int32)
-  : IBlake2STreeConfig;
+procedure TBlake2STreeConfig.ValidateFanOut(AFanOut: Byte);
 begin
 begin
-  result := TBlake2STreeConfig.Create();
-  result.FanOut := parallelism;
-  result.MaxHeight := 2;
-  result.IntermediateHashSize := 32;
+  if not(AFanOut in [0 .. 255]) then
+  begin
+    raise EArgumentInvalidHashLibException.CreateRes(@SInvalidFanOutParameter);
+  end;
+end;
+
+procedure TBlake2STreeConfig.ValidateInnerHashSize(AInnerHashSize: Byte);
+begin
+  if not(AInnerHashSize in [0 .. 32]) then
+  begin
+    raise EArgumentInvalidHashLibException.CreateRes
+      (@SInvalidInnerHashSizeParameter);
+  end;
+end;
+
+procedure TBlake2STreeConfig.ValidateMaxDepth(AMaxDepth: Byte);
+begin
+  if not(AMaxDepth in [1 .. 255]) then
+  begin
+    raise EArgumentInvalidHashLibException.CreateRes
+      (@SInvalidMaxDepthParameter);
+  end;
 end;
 end;
 
 
-function TBlake2STreeConfig.GetFanOut: Int32;
+procedure TBlake2STreeConfig.ValidateNodeDepth(ANodeDepth: Byte);
+begin
+  if not(ANodeDepth in [0 .. 255]) then
+  begin
+    raise EArgumentInvalidHashLibException.CreateRes
+      (@SInvalidNodeDepthParameter);
+  end;
+end;
+
+procedure TBlake2STreeConfig.ValidateNodeOffset(ANodeOffset: UInt64);
+begin
+  if ANodeOffset > UInt64((UInt64(1) shl 48) - 1) then
+  begin
+    raise EArgumentInvalidHashLibException.CreateRes
+      (@SInvalidNodeOffsetParameter);
+  end;
+end;
+
+function TBlake2STreeConfig.GetFanOut: Byte;
 begin
 begin
   result := FFanOut;
   result := FFanOut;
 end;
 end;
 
 
-function TBlake2STreeConfig.GetIntermediateHashSize: Int32;
+function TBlake2STreeConfig.GetInnerHashSize: Byte;
+begin
+  result := FInnerHashSize;
+end;
+
+function TBlake2STreeConfig.GetIsLastNode: Boolean;
 begin
 begin
-  result := FIntermediateHashSize;
+  result := FIsLastNode;
 end;
 end;
 
 
-function TBlake2STreeConfig.GetLeafSize: Int64;
+function TBlake2STreeConfig.GetLeafSize: UInt32;
 begin
 begin
   result := FLeafSize;
   result := FLeafSize;
 end;
 end;
 
 
-function TBlake2STreeConfig.GetMaxHeight: Int32;
+function TBlake2STreeConfig.GetMaxDepth: Byte;
+begin
+  result := FMaxDepth;
+end;
+
+function TBlake2STreeConfig.GetNodeDepth: Byte;
 begin
 begin
-  result := FMaxHeight;
+  result := FNodeDepth;
 end;
 end;
 
 
-procedure TBlake2STreeConfig.SetFanOut(value: Int32);
+function TBlake2STreeConfig.GetNodeOffset: UInt64;
 begin
 begin
+  result := FNodeOffset;
+end;
+
+procedure TBlake2STreeConfig.SetFanOut(value: Byte);
+begin
+  ValidateFanOut(value);
   FFanOut := value;
   FFanOut := value;
 end;
 end;
 
 
-procedure TBlake2STreeConfig.SetIntermediateHashSize(value: Int32);
+procedure TBlake2STreeConfig.SetInnerHashSize(value: Byte);
+begin
+  ValidateInnerHashSize(value);
+  FInnerHashSize := value;
+end;
+
+procedure TBlake2STreeConfig.SetIsLastNode(value: Boolean);
 begin
 begin
-  FIntermediateHashSize := value;
+  FIsLastNode := value;
 end;
 end;
 
 
-procedure TBlake2STreeConfig.SetLeafSize(value: Int64);
+procedure TBlake2STreeConfig.SetLeafSize(value: UInt32);
 begin
 begin
   FLeafSize := value;
   FLeafSize := value;
 end;
 end;
 
 
-procedure TBlake2STreeConfig.SetMaxHeight(value: Int32);
+procedure TBlake2STreeConfig.SetMaxDepth(value: Byte);
+begin
+  ValidateMaxDepth(value);
+  FMaxDepth := value;
+end;
+
+procedure TBlake2STreeConfig.SetNodeDepth(value: Byte);
+begin
+  ValidateNodeDepth(value);
+  FNodeDepth := value;
+end;
+
+procedure TBlake2STreeConfig.SetNodeOffset(value: UInt64);
 begin
 begin
-  FMaxHeight := value;
+  ValidateNodeOffset(value);
+  FNodeOffset := value;
 end;
 end;
 
 
 constructor TBlake2STreeConfig.Create;
 constructor TBlake2STreeConfig.Create;
 begin
 begin
   Inherited Create();
   Inherited Create();
-  FIntermediateHashSize := 32;
+  ValidateInnerHashSize(32);
+  FInnerHashSize := 32;
 end;
 end;
 
 
 end.
 end.

+ 2 - 0
src/libraries/hashlib4pascal/HlpCRC.pas

@@ -1,5 +1,6 @@
 unit HlpCRC;
 unit HlpCRC;
 
 
+
 // A vast majority if not all of the parameters for these CRC standards
 // A vast majority if not all of the parameters for these CRC standards
 // were gotten from http://reveng.sourceforge.net/crc-catalogue/.
 // were gotten from http://reveng.sourceforge.net/crc-catalogue/.
 
 
@@ -1456,3 +1457,4 @@ begin
 end;
 end;
 
 
 end.
 end.
+

+ 65 - 38
src/libraries/hashlib4pascal/HlpHashFactory.pas

@@ -84,9 +84,11 @@ uses
   HlpBlake2B,
   HlpBlake2B,
   HlpIBlake2BConfig,
   HlpIBlake2BConfig,
   HlpBlake2BConfig,
   HlpBlake2BConfig,
+  HlpIBlake2BTreeConfig,
   HlpBlake2S,
   HlpBlake2S,
   HlpBlake2SConfig,
   HlpBlake2SConfig,
   HlpIBlake2SConfig,
   HlpIBlake2SConfig,
+  HlpIBlake2STreeConfig,
   // HMAC Unit
   // HMAC Unit
   HlpHMACNotBuildInAdapter,
   HlpHMACNotBuildInAdapter,
   // PBKDF2_HMAC Unit
   // PBKDF2_HMAC Unit
@@ -179,7 +181,7 @@ type
       class function CreateFNV(): IHash; static;
       class function CreateFNV(): IHash; static;
       class function CreateFNV1a(): IHash; static;
       class function CreateFNV1a(): IHash; static;
 
 
-      class function CreateJenkins3(): IHash; static;
+      class function CreateJenkins3(AInitialValue: Int32 = 0): IHash; static;
 
 
       class function CreateJS(): IHash; static;
       class function CreateJS(): IHash; static;
 
 
@@ -370,22 +372,20 @@ type
 
 
       class function CreateKeccak_224(): IHash; static;
       class function CreateKeccak_224(): IHash; static;
       class function CreateKeccak_256(): IHash; static;
       class function CreateKeccak_256(): IHash; static;
+      class function CreateKeccak_288(): IHash; static;
       class function CreateKeccak_384(): IHash; static;
       class function CreateKeccak_384(): IHash; static;
       class function CreateKeccak_512(): IHash; static;
       class function CreateKeccak_512(): IHash; static;
 
 
-      class function CreateShake_128(a_xof_size_in_bits: Int32): IHash; static;
-      class function CreateShake_256(a_xof_size_in_bits: Int32): IHash; static;
-
-      class function CreateBlake2B(const config: IBlake2BConfig = Nil)
-        : IHash; static;
+      class function CreateBlake2B(const config: IBlake2BConfig = Nil;
+        const treeConfig: IBlake2BTreeConfig = Nil): IHash; static;
 
 
       class function CreateBlake2B_160(): IHash; static;
       class function CreateBlake2B_160(): IHash; static;
       class function CreateBlake2B_256(): IHash; static;
       class function CreateBlake2B_256(): IHash; static;
       class function CreateBlake2B_384(): IHash; static;
       class function CreateBlake2B_384(): IHash; static;
       class function CreateBlake2B_512(): IHash; static;
       class function CreateBlake2B_512(): IHash; static;
 
 
-      class function CreateBlake2S(const config: IBlake2SConfig = Nil)
-        : IHash; static;
+      class function CreateBlake2S(const config: IBlake2SConfig = Nil;
+        const treeConfig: IBlake2STreeConfig = Nil): IHash; static;
 
 
       class function CreateBlake2S_128(): IHash; static;
       class function CreateBlake2S_128(): IHash; static;
       class function CreateBlake2S_160(): IHash; static;
       class function CreateBlake2S_160(): IHash; static;
@@ -394,6 +394,18 @@ type
 
 
     end;
     end;
 
 
+    // ====================== TXOF ====================== //
+
+  type
+    TXOF = class sealed(TObject)
+
+    public
+
+      class function CreateShake_128(a_xof_size_in_bits: UInt32): IHash; static;
+      class function CreateShake_256(a_xof_size_in_bits: UInt32): IHash; static;
+
+    end;
+
     // ====================== THMAC ====================== //
     // ====================== THMAC ====================== //
 
 
   type
   type
@@ -604,9 +616,9 @@ begin
   Result := TFNV1a.Create();
   Result := TFNV1a.Create();
 end;
 end;
 
 
-class function THashFactory.THash32.CreateJenkins3: IHash;
+class function THashFactory.THash32.CreateJenkins3(AInitialValue: Int32): IHash;
 begin
 begin
-  Result := TJenkins3.Create();
+  Result := TJenkins3.Create(AInitialValue);
 end;
 end;
 
 
 class function THashFactory.THash32.CreateJS: IHash;
 class function THashFactory.THash32.CreateJS: IHash;
@@ -982,18 +994,6 @@ begin
   Result := TSHA3_512.Create();
   Result := TSHA3_512.Create();
 end;
 end;
 
 
-class function THashFactory.TCrypto.CreateShake_128(a_xof_size_in_bits
-  : Int32): IHash;
-begin
-  Result := (TShake_128.Create() as IXOF).SetXOFOutputSize(a_xof_size_in_bits);
-end;
-
-class function THashFactory.TCrypto.CreateShake_256(a_xof_size_in_bits
-  : Int32): IHash;
-begin
-  Result := (TShake_256.Create() as IXOF).SetXOFOutputSize(a_xof_size_in_bits);
-end;
-
 class function THashFactory.TCrypto.CreateKeccak_224: IHash;
 class function THashFactory.TCrypto.CreateKeccak_224: IHash;
 begin
 begin
   Result := TKeccak_224.Create();
   Result := TKeccak_224.Create();
@@ -1004,6 +1004,11 @@ begin
   Result := TKeccak_256.Create();
   Result := TKeccak_256.Create();
 end;
 end;
 
 
+class function THashFactory.TCrypto.CreateKeccak_288: IHash;
+begin
+  Result := TKeccak_288.Create();
+end;
+
 class function THashFactory.TCrypto.CreateKeccak_384: IHash;
 class function THashFactory.TCrypto.CreateKeccak_384: IHash;
 begin
 begin
   Result := TKeccak_384.Create();
   Result := TKeccak_384.Create();
@@ -1014,17 +1019,17 @@ begin
   Result := TKeccak_512.Create();
   Result := TKeccak_512.Create();
 end;
 end;
 
 
-class function THashFactory.TCrypto.CreateBlake2B(const config
-  : IBlake2BConfig): IHash;
+class function THashFactory.TCrypto.CreateBlake2B(const config: IBlake2BConfig;
+  const treeConfig: IBlake2BTreeConfig): IHash;
+var
+  LConfig: IBlake2BConfig;
 begin
 begin
-  if config = Nil then
-  begin
-    Result := TBlake2B.Create()
-  end
-  else
+  LConfig := config;
+  if (LConfig = Nil) then
   begin
   begin
-    Result := TBlake2B.Create(config);
+    LConfig := TBlake2BConfig.Create();
   end;
   end;
+  Result := TBlake2B.Create(LConfig, treeConfig);
 end;
 end;
 
 
 class function THashFactory.TCrypto.CreateBlake2B_160: IHash;
 class function THashFactory.TCrypto.CreateBlake2B_160: IHash;
@@ -1051,17 +1056,17 @@ begin
     (TBlake2BConfig.Create(THashSize.hsHashSize512));
     (TBlake2BConfig.Create(THashSize.hsHashSize512));
 end;
 end;
 
 
-class function THashFactory.TCrypto.CreateBlake2S(const config
-  : IBlake2SConfig): IHash;
+class function THashFactory.TCrypto.CreateBlake2S(const config: IBlake2SConfig;
+  const treeConfig: IBlake2STreeConfig): IHash;
+var
+  LConfig: IBlake2SConfig;
 begin
 begin
-  if config = Nil then
-  begin
-    Result := TBlake2S.Create()
-  end
-  else
+  LConfig := config;
+  if (LConfig = Nil) then
   begin
   begin
-    Result := TBlake2S.Create(config);
+    LConfig := TBlake2SConfig.Create();
   end;
   end;
+  Result := TBlake2S.Create(LConfig, treeConfig);
 end;
 end;
 
 
 class function THashFactory.TCrypto.CreateBlake2S_128: IHash;
 class function THashFactory.TCrypto.CreateBlake2S_128: IHash;
@@ -1229,6 +1234,28 @@ begin
   Result := TTiger2_192.CreateRound5();
   Result := TTiger2_192.CreateRound5();
 end;
 end;
 
 
+{ THashFactory.TXOF }
+
+class function THashFactory.TXOF.CreateShake_128(a_xof_size_in_bits
+  : UInt32): IHash;
+var
+  LXof: IXOF;
+begin
+  LXof := (TShake_128.Create() as IXOF);
+  LXof.XOFSizeInBits := a_xof_size_in_bits;
+  Result := LXof as IHash;
+end;
+
+class function THashFactory.TXOF.CreateShake_256(a_xof_size_in_bits
+  : UInt32): IHash;
+var
+  LXof: IXOF;
+begin
+  LXof := (TShake_256.Create() as IXOF);
+  LXof.XOFSizeInBits := a_xof_size_in_bits;
+  Result := LXof as IHash;
+end;
+
 { THashFactory.THMAC }
 { THashFactory.THMAC }
 
 
 class function THashFactory.THMAC.CreateHMAC(const a_hash: IHash;
 class function THashFactory.THMAC.CreateHMAC(const a_hash: IHash;

+ 1 - 1
src/libraries/hashlib4pascal/HlpHashSize.pas

@@ -7,7 +7,7 @@ interface
 type
 type
 {$SCOPEDENUMS ON}
 {$SCOPEDENUMS ON}
   THashSize = (hsHashSize128 = 16, hsHashSize160 = 20, hsHashSize192 = 24,
   THashSize = (hsHashSize128 = 16, hsHashSize160 = 20, hsHashSize192 = 24,
-    hsHashSize224 = 28, hsHashSize256 = 32, hsHashSize384 = 48,
+    hsHashSize224 = 28, hsHashSize256 = 32, hsHashSize288 = 36, hsHashSize384 = 48,
     hsHashSize512 = 64);
     hsHashSize512 = 64);
 {$SCOPEDENUMS OFF}
 {$SCOPEDENUMS OFF}
 
 

+ 28 - 13
src/libraries/hashlib4pascal/HlpIBlake2BTreeConfig.pas

@@ -7,19 +7,34 @@ interface
 type
 type
   IBlake2BTreeConfig = interface(IInterface)
   IBlake2BTreeConfig = interface(IInterface)
     ['{3EFB1A70-4478-4375-BAF6-EF17B3673DA8}']
     ['{3EFB1A70-4478-4375-BAF6-EF17B3673DA8}']
-    function GetIntermediateHashSize: Int32;
-    procedure SetIntermediateHashSize(value: Int32);
-    property IntermediateHashSize: Int32 read GetIntermediateHashSize
-      write SetIntermediateHashSize;
-    function GetMaxHeight: Int32;
-    procedure SetMaxHeight(value: Int32);
-    property MaxHeight: Int32 read GetMaxHeight write SetMaxHeight;
-    function GetLeafSize: Int64;
-    procedure SetLeafSize(value: Int64);
-    property LeafSize: Int64 read GetLeafSize write SetLeafSize;
-    function GetFanOut: Int32;
-    procedure SetFanOut(value: Int32);
-    property FanOut: Int32 read GetFanOut write SetFanOut;
+
+    function GetFanOut: Byte;
+    procedure SetFanOut(value: Byte);
+    property FanOut: Byte read GetFanOut write SetFanOut;
+
+    function GetMaxDepth: Byte;
+    procedure SetMaxDepth(value: Byte);
+    property MaxDepth: Byte read GetMaxDepth write SetMaxDepth;
+
+    function GetNodeDepth: Byte;
+    procedure SetNodeDepth(value: Byte);
+    property NodeDepth: Byte read GetNodeDepth write SetNodeDepth;
+
+    function GetInnerHashSize: Byte;
+    procedure SetInnerHashSize(value: Byte);
+    property InnerHashSize: Byte read GetInnerHashSize write SetInnerHashSize;
+
+    function GetLeafSize: UInt32;
+    procedure SetLeafSize(value: UInt32);
+    property LeafSize: UInt32 read GetLeafSize write SetLeafSize;
+
+    function GetNodeOffset: UInt64;
+    procedure SetNodeOffset(value: UInt64);
+    property NodeOffset: UInt64 read GetNodeOffset write SetNodeOffset;
+
+    function GetIsLastNode: Boolean;
+    procedure SetIsLastNode(value: Boolean);
+    property IsLastNode: Boolean read GetIsLastNode write SetIsLastNode;
 
 
   end;
   end;
 
 

+ 29 - 14
src/libraries/hashlib4pascal/HlpIBlake2STreeConfig.pas

@@ -6,20 +6,35 @@ interface
 
 
 type
 type
   IBlake2STreeConfig = interface(IInterface)
   IBlake2STreeConfig = interface(IInterface)
-    ['{9B0E3927-6C85-46C4-8EFE-609D1FF24030}']
-    function GetIntermediateHashSize: Int32;
-    procedure SetIntermediateHashSize(value: Int32);
-    property IntermediateHashSize: Int32 read GetIntermediateHashSize
-      write SetIntermediateHashSize;
-    function GetMaxHeight: Int32;
-    procedure SetMaxHeight(value: Int32);
-    property MaxHeight: Int32 read GetMaxHeight write SetMaxHeight;
-    function GetLeafSize: Int64;
-    procedure SetLeafSize(value: Int64);
-    property LeafSize: Int64 read GetLeafSize write SetLeafSize;
-    function GetFanOut: Int32;
-    procedure SetFanOut(value: Int32);
-    property FanOut: Int32 read GetFanOut write SetFanOut;
+    ['{93635D4F-7104-4E4A-BE9B-C608606F620F}']
+
+    function GetFanOut: Byte;
+    procedure SetFanOut(value: Byte);
+    property FanOut: Byte read GetFanOut write SetFanOut;
+
+    function GetMaxDepth: Byte;
+    procedure SetMaxDepth(value: Byte);
+    property MaxDepth: Byte read GetMaxDepth write SetMaxDepth;
+
+    function GetNodeDepth: Byte;
+    procedure SetNodeDepth(value: Byte);
+    property NodeDepth: Byte read GetNodeDepth write SetNodeDepth;
+
+    function GetInnerHashSize: Byte;
+    procedure SetInnerHashSize(value: Byte);
+    property InnerHashSize: Byte read GetInnerHashSize write SetInnerHashSize;
+
+    function GetLeafSize: UInt32;
+    procedure SetLeafSize(value: UInt32);
+    property LeafSize: UInt32 read GetLeafSize write SetLeafSize;
+
+    function GetNodeOffset: UInt64;
+    procedure SetNodeOffset(value: UInt64);
+    property NodeOffset: UInt64 read GetNodeOffset write SetNodeOffset;
+
+    function GetIsLastNode: Boolean;
+    procedure SetIsLastNode(value: Boolean);
+    property IsLastNode: Boolean read GetIsLastNode write SetIsLastNode;
 
 
   end;
   end;
 
 

+ 3 - 4
src/libraries/hashlib4pascal/HlpIHashInfo.pas

@@ -94,10 +94,9 @@ type
 
 
   IXOF = Interface(IHash)
   IXOF = Interface(IHash)
     ['{944ED7F0-D033-4489-A5DD-9C83353F23F0}']
     ['{944ED7F0-D033-4489-A5DD-9C83353F23F0}']
-    function GetXOFSize: Int32;
-    procedure SetXOFSize(a_xof_size_in_bits: Int32);
-    property XOFSize: Int32 read GetXOFSize write SetXOFSize;
-    function SetXOFOutputSize(a_xof_size_in_bits: Int32): IXOF;
+    function GetXOFSizeInBits: UInt32;
+    procedure SetXOFSizeInBits(a_xof_size_in_bits: UInt32);
+    property XOFSizeInBits: UInt32 read GetXOFSizeInBits write SetXOFSizeInBits;
   end;
   end;
 
 
 type
 type

+ 12 - 6
src/libraries/hashlib4pascal/HlpJenkins3.pas

@@ -19,12 +19,15 @@ uses
 type
 type
 
 
   TJenkins3 = class sealed(TMultipleTransformNonBlock, IHash32, ITransformBlock)
   TJenkins3 = class sealed(TMultipleTransformNonBlock, IHash32, ITransformBlock)
+  strict private
+  var
+    FInitialValue: Int32;
 
 
   strict protected
   strict protected
     function ComputeAggregatedBytes(const a_data: THashLibByteArray)
     function ComputeAggregatedBytes(const a_data: THashLibByteArray)
       : IHashResult; override;
       : IHashResult; override;
   public
   public
-    constructor Create();
+    constructor Create(AInitialValue: Int32 = 0);
     function Clone(): IHash; override;
     function Clone(): IHash; override;
 
 
   end;
   end;
@@ -33,9 +36,10 @@ implementation
 
 
 { TJenkins3 }
 { TJenkins3 }
 
 
-constructor TJenkins3.Create;
+constructor TJenkins3.Create(AInitialValue: Int32);
 begin
 begin
   Inherited Create(4, 12);
   Inherited Create(4, 12);
+  FInitialValue := AInitialValue;
 end;
 end;
 
 
 function TJenkins3.Clone(): IHash;
 function TJenkins3.Clone(): IHash;
@@ -44,6 +48,7 @@ var
 begin
 begin
   HashInstance := TJenkins3.Create();
   HashInstance := TJenkins3.Create();
   FBuffer.Position := 0;
   FBuffer.Position := 0;
+  HashInstance.FInitialValue := FInitialValue;
   HashInstance.FBuffer.CopyFrom(FBuffer, FBuffer.Size);
   HashInstance.FBuffer.CopyFrom(FBuffer, FBuffer.Size);
   result := HashInstance as IHash;
   result := HashInstance as IHash;
   result.BufferSize := BufferSize;
   result.BufferSize := BufferSize;
@@ -56,14 +61,15 @@ var
   a, b, c: UInt32;
   a, b, c: UInt32;
 begin
 begin
   length := System.length(a_data);
   length := System.length(a_data);
+  a := UInt32($DEADBEEF) + UInt32(length) + UInt32(FInitialValue);
+  b := a;
+  c := b;
   if (length = 0) then
   if (length = 0) then
   begin
   begin
-    result := THashResult.Create(UInt32(0));
+    result := THashResult.Create(c);
     Exit;
     Exit;
   end;
   end;
-  a := $DEADBEEF + UInt32(length);
-  b := a;
-  c := b;
+
   currentIndex := 0;
   currentIndex := 0;
   while (length > 12) do
   while (length > 12) do
   begin
   begin

File diff suppressed because it is too large
+ 224 - 2595
src/libraries/hashlib4pascal/HlpSHA3.pas


+ 1 - 0
src/libraries/hashlib4pascal/HlpSnefru.pas

@@ -855,3 +855,4 @@ begin
 end;
 end;
 
 
 end.
 end.
+

Some files were not shown because too many files changed in this diff