|
@@ -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.
|