Browse Source

minor interface change for Argon2ParametersGenerator class

Ugochukwu Mmaduekwe 6 years ago
parent
commit
49b4b53354

+ 4 - 2
CryptoLib.Tests/src/Crypto/Argon2Tests.pas

@@ -102,7 +102,8 @@ begin
   //
   //
 
 
   LArgon2Generator.Init(AArgon2Type, AArgon2Version, LPassword, LSalt, LSecret,
   LArgon2Generator.Init(AArgon2Type, AArgon2Version, LPassword, LSalt, LSecret,
-    LAdditional, AIterations, AMemoryAsKB, AParallelism, True);
+    LAdditional, AIterations, AMemoryAsKB, AParallelism,
+    TArgon2MemoryCostType.a2mctMemoryAsKB);
 
 
   LActual := TConverters.ConvertBytesToHexString
   LActual := TConverters.ConvertBytesToHexString
     ((LArgon2Generator.GenerateDerivedMacParameters(AOutputLength)
     ((LArgon2Generator.GenerateDerivedMacParameters(AOutputLength)
@@ -132,7 +133,8 @@ begin
   //
   //
 
 
   LArgon2Generator.Init(AArgon2Type, AArgon2Version, LPassword, LSalt, Nil, Nil,
   LArgon2Generator.Init(AArgon2Type, AArgon2Version, LPassword, LSalt, Nil, Nil,
-    AIterations, AMemory, AParallelism, False);
+    AIterations, AMemory, AParallelism,
+    TArgon2MemoryCostType.a2mctMemoryPowOfTwo);
 
 
   LActual := TConverters.ConvertBytesToHexString
   LActual := TConverters.ConvertBytesToHexString
     ((LArgon2Generator.GenerateDerivedMacParameters(AOutputLength)
     ((LArgon2Generator.GenerateDerivedMacParameters(AOutputLength)

+ 24 - 12
CryptoLib/src/Crypto/Generators/ClpArgon2ParametersGenerator.pas

@@ -36,6 +36,7 @@ uses
 
 
 resourcestring
 resourcestring
   SArgon2TypeInvalid = 'Selected Argon2Type is Invalid';
   SArgon2TypeInvalid = 'Selected Argon2Type is Invalid';
+  SArgon2MemoryCostTypeInvalid = 'Selected Argon2MemoryCostType is Invalid';
 
 
 type
 type
 
 
@@ -78,7 +79,8 @@ type
 
 
     procedure Init(argon2Type: TArgon2Type; argon2Version: TArgon2Version;
     procedure Init(argon2Type: TArgon2Type; argon2Version: TArgon2Version;
       const password, salt, secret, additional: TCryptoLibByteArray;
       const password, salt, secret, additional: TCryptoLibByteArray;
-      iterations, memory, parallelism: Int32; memoryAsKB: Boolean);
+      iterations, memory, parallelism: Int32;
+      memoryCostType: TArgon2MemoryCostType);
 
 
     /// <summary>
     /// <summary>
     /// Generate a key parameter derived from the password, salt, and
     /// Generate a key parameter derived from the password, salt, and
@@ -211,7 +213,7 @@ end;
 procedure TArgon2ParametersGenerator.Init(argon2Type: TArgon2Type;
 procedure TArgon2ParametersGenerator.Init(argon2Type: TArgon2Type;
   argon2Version: TArgon2Version; const password, salt, secret,
   argon2Version: TArgon2Version; const password, salt, secret,
   additional: TCryptoLibByteArray; iterations, memory, parallelism: Int32;
   additional: TCryptoLibByteArray; iterations, memory, parallelism: Int32;
-  memoryAsKB: Boolean);
+  memoryCostType: TArgon2MemoryCostType);
 var
 var
   LArgon2ParametersBuilder: IArgon2ParametersBuilder;
   LArgon2ParametersBuilder: IArgon2ParametersBuilder;
 begin
 begin
@@ -237,17 +239,27 @@ begin
     end;
     end;
   end;
   end;
 
 
-  if memoryAsKB then
-  begin
-    LArgon2ParametersBuilder.WithVersion(argon2Version).WithSalt(salt)
-      .WithSecret(secret).WithAdditional(additional).WithIterations(iterations)
-      .WithMemoryAsKB(memory).WithParallelism(parallelism);
-  end
+  case memoryCostType of
+    TArgon2MemoryCostType.a2mctMemoryAsKB:
+      begin
+        LArgon2ParametersBuilder.WithVersion(argon2Version).WithSalt(salt)
+          .WithSecret(secret).WithAdditional(additional)
+          .WithIterations(iterations).WithMemoryAsKB(memory)
+          .WithParallelism(parallelism);
+      end;
+
+    TArgon2MemoryCostType.a2mctMemoryPowOfTwo:
+      begin
+        LArgon2ParametersBuilder.WithVersion(argon2Version).WithSalt(salt)
+          .WithSecret(secret).WithAdditional(additional)
+          .WithIterations(iterations).WithMemoryPowOfTwo(memory)
+          .WithParallelism(parallelism);
+      end
   else
   else
-  begin
-    LArgon2ParametersBuilder.WithVersion(argon2Version).WithSalt(salt)
-      .WithSecret(secret).WithAdditional(additional).WithIterations(iterations)
-      .WithMemoryPowOfTwo(memory).WithParallelism(parallelism);
+    begin
+      raise EArgumentCryptoLibException.CreateRes
+        (@SArgon2MemoryCostTypeInvalid);
+    end;
   end;
   end;
 
 
   FPBKDF_Argon2 := TKDF.TPBKDF_Argon2.CreatePBKDF_Argon2(FPassword,
   FPBKDF_Argon2 := TKDF.TPBKDF_Argon2.CreatePBKDF_Argon2(FPassword,

+ 3 - 1
CryptoLib/src/Interfaces/ClpIArgon2ParametersGenerator.pas

@@ -31,6 +31,7 @@ type
 {$SCOPEDENUMS ON}
 {$SCOPEDENUMS ON}
   TArgon2Type = HlpArgon2TypeAndVersion.TArgon2Type;
   TArgon2Type = HlpArgon2TypeAndVersion.TArgon2Type;
   TArgon2Version = HlpArgon2TypeAndVersion.TArgon2Version;
   TArgon2Version = HlpArgon2TypeAndVersion.TArgon2Version;
+  TArgon2MemoryCostType = (a2mctMemoryAsKB, a2mctMemoryPowOfTwo);
 {$SCOPEDENUMS OFF}
 {$SCOPEDENUMS OFF}
 
 
 type
 type
@@ -40,7 +41,8 @@ type
 
 
     procedure Init(argon2Type: TArgon2Type; argon2Version: TArgon2Version;
     procedure Init(argon2Type: TArgon2Type; argon2Version: TArgon2Version;
       const password, salt, secret, additional: TCryptoLibByteArray;
       const password, salt, secret, additional: TCryptoLibByteArray;
-      iterations, memory, parallelism: Int32; memoryAsKB: Boolean);
+      iterations, memory, parallelism: Int32;
+      memoryCostType: TArgon2MemoryCostType);
 
 
     /// <returns>
     /// <returns>
     /// the password byte array.
     /// the password byte array.