Browse Source

Relax constraint on hashsize for Blake2b/Blake2s

Relax constraint on hashsize for Blake2b/Blake2s to reflect the Blake2 specification fully.
Ugochukwu Mmaduekwe 7 years ago
parent
commit
25d7986c44

+ 1 - 25
src/libraries/hashlib4pascal/HlpBlake2B.pas

@@ -16,7 +16,6 @@ uses
 {$ENDIF DELPHI}
   HlpBits,
   HlpHash,
-  HlpHashSize,
   HlpHashResult,
   HlpIHashResult,
   HlpIBlake2BConfig,
@@ -30,8 +29,6 @@ uses
 
 resourcestring
   SInvalidConfigLength = 'Config Length Must Be 8 Words';
-  SInvalidHashSize =
-    'BLAKE2B HashSize must be restricted to one of the following [20, 32, 48, 64], "%d"';
 
 type
   TBlake2B = class sealed(THash, ICryptoNotBuildIn, ITransformBlock)
@@ -90,8 +87,6 @@ type
 
     procedure Finish(); inline;
 
-    function GetHashSize(AHashSize: Int32): THashSize; inline;
-
   strict protected
 
     function GetName: String; override;
@@ -113,25 +108,6 @@ implementation
 
 { TBlake2B }
 
-function TBlake2B.GetHashSize(AHashSize: Int32): THashSize;
-begin
-  case AHashSize of
-    20:
-      Result := THashSize.hsHashSize160;
-    32:
-      Result := THashSize.hsHashSize256;
-    48:
-      Result := THashSize.hsHashSize384;
-    64:
-      Result := THashSize.hsHashSize512;
-  else
-    begin
-      raise EArgumentInvalidHashLibException.CreateResFmt(@SInvalidHashSize,
-        [AHashSize]);
-    end;
-  end;
-end;
-
 class constructor TBlake2B.Blake2BConfig;
 begin
   FDefaultConfig := TBlake2BConfig.Create();
@@ -168,7 +144,7 @@ function TBlake2B.Clone(): IHash;
 var
   HashInstance: TBlake2B;
 begin
-  HashInstance := TBlake2B.Create(TBlake2BConfig.Create(GetHashSize(FHashSize))
+  HashInstance := TBlake2B.Create(TBlake2BConfig.Create(FHashSize)
     as IBlake2BConfig);
   System.Move(F_m, HashInstance.F_m, System.SizeOf(F_m));
   HashInstance.FrawConfig := System.Copy(FrawConfig);

+ 12 - 3
src/libraries/hashlib4pascal/HlpBlake2BConfig.pas

@@ -11,7 +11,7 @@ uses
 
 resourcestring
   SInvalidHashSize =
-    'BLAKE2B HashSize must be restricted to one of the following [20, 32, 48, 64], "%d"';
+    'BLAKE2B HashSize must be restricted to one of the following [1 .. 64], "%d"';
   SInvalidKeyLength = '"Key" Length Must Not Be Greater Than 64, "%d"';
   SInvalidPersonalisationLength =
     '"Personalisation" Length Must Be Equal To 16, "%d"';
@@ -48,6 +48,8 @@ type
 
   public
     constructor Create(AHashSize: THashSize = THashSize.hsHashSize512);
+      overload;
+    constructor Create(AHashSize: Int32); overload;
     property Personalisation: THashLibByteArray read GetPersonalisation
       write SetPersonalisation;
     property Salt: THashLibByteArray read GetSalt write SetSalt;
@@ -62,10 +64,10 @@ implementation
 
 procedure TBlake2BConfig.ValidateHashSize(AHashSize: Int32);
 begin
-  if not((AHashSize) in [20, 32, 48, 64]) then
+  if not((AHashSize) in [1 .. 64]) or (((AHashSize * 8) and 7) <> 0) then
   begin
     raise EArgumentHashLibException.CreateResFmt(@SInvalidHashSize,
-      [Int32(AHashSize)]);
+      [AHashSize]);
   end;
 end;
 
@@ -169,4 +171,11 @@ begin
   FHashSize := LHashSize;
 end;
 
+constructor TBlake2BConfig.Create(AHashSize: Int32);
+begin
+  Inherited Create();
+  ValidateHashSize(AHashSize);
+  FHashSize := AHashSize;
+end;
+
 end.

+ 1 - 25
src/libraries/hashlib4pascal/HlpBlake2S.pas

@@ -16,7 +16,6 @@ uses
 {$ENDIF DELPHI}
   HlpBits,
   HlpHash,
-  HlpHashSize,
   HlpHashResult,
   HlpIHashResult,
   HlpIBlake2SConfig,
@@ -30,8 +29,6 @@ uses
 
 resourcestring
   SInvalidConfigLength = 'Config Length Must Be 8 Words';
-  SInvalidHashSize =
-    'BLAKE2S HashSize must be restricted to one of the following [16, 20, 28, 32], "%d"';
 
 type
   TBlake2S = class sealed(THash, ICryptoNotBuildIn, ITransformBlock)
@@ -90,8 +87,6 @@ type
 
     procedure Finish(); inline;
 
-    function GetHashSize(AHashSize: Int32): THashSize; inline;
-
   strict protected
 
     function GetName: String; override;
@@ -113,25 +108,6 @@ implementation
 
 { TBlake2S }
 
-function TBlake2S.GetHashSize(AHashSize: Int32): THashSize;
-begin
-  case AHashSize of
-    16:
-      Result := THashSize.hsHashSize128;
-    20:
-      Result := THashSize.hsHashSize160;
-    28:
-      Result := THashSize.hsHashSize224;
-    32:
-      Result := THashSize.hsHashSize256;
-  else
-    begin
-      raise EArgumentInvalidHashLibException.CreateResFmt(@SInvalidHashSize,
-        [AHashSize]);
-    end;
-  end;
-end;
-
 class constructor TBlake2S.Blake2SConfig;
 begin
   FDefaultConfig := TBlake2SConfig.Create();
@@ -163,7 +139,7 @@ function TBlake2S.Clone(): IHash;
 var
   HashInstance: TBlake2S;
 begin
-  HashInstance := TBlake2S.Create(TBlake2SConfig.Create(GetHashSize(FHashSize))
+  HashInstance := TBlake2S.Create(TBlake2SConfig.Create(FHashSize)
     as IBlake2SConfig);
   System.Move(F_m, HashInstance.F_m, System.SizeOf(F_m));
   HashInstance.FrawConfig := System.Copy(FrawConfig);

+ 12 - 3
src/libraries/hashlib4pascal/HlpBlake2SConfig.pas

@@ -11,7 +11,7 @@ uses
 
 resourcestring
   SInvalidHashSize =
-    'BLAKE2S HashSize must be restricted to one of the following [16, 20, 28, 32], "%d"';
+    'BLAKE2S HashSize must be restricted to one of the following [1 .. 32], "%d"';
   SInvalidKeyLength = '"Key" Length Must Not Be Greater Than 32, "%d"';
   SInvalidPersonalisationLength =
     '"Personalisation" Length Must Be Equal To 8, "%d"';
@@ -48,6 +48,8 @@ type
 
   public
     constructor Create(AHashSize: THashSize = THashSize.hsHashSize256);
+      overload;
+    constructor Create(AHashSize: Int32); overload;
     property Personalisation: THashLibByteArray read GetPersonalisation
       write SetPersonalisation;
     property Salt: THashLibByteArray read GetSalt write SetSalt;
@@ -62,10 +64,10 @@ implementation
 
 procedure TBlake2SConfig.ValidateHashSize(AHashSize: Int32);
 begin
-  if not((AHashSize) in [16, 20, 28, 32]) then
+  if not((AHashSize) in [1 .. 32]) or (((AHashSize * 8) and 7) <> 0) then
   begin
     raise EArgumentHashLibException.CreateResFmt(@SInvalidHashSize,
-      [Int32(AHashSize)]);
+      [AHashSize]);
   end;
 end;
 
@@ -169,4 +171,11 @@ begin
   FHashSize := LHashSize;
 end;
 
+constructor TBlake2SConfig.Create(AHashSize: Int32);
+begin
+  Inherited Create();
+  ValidateHashSize(AHashSize);
+  FHashSize := AHashSize;
+end;
+
 end.