Jelajahi Sumber

add keccak hash to digestutilities

Ugochukwu Mmaduekwe 6 tahun lalu
induk
melakukan
a49995e3e1

+ 24 - 0
CryptoLib.Tests/src/Crypto/HMacTests.pas

@@ -63,6 +63,7 @@ type
     FkeyBytes, Fmessage, Foutput1, FoutputMD5, FoutputMD2, FoutputMD4,
       Foutput224, Foutput256, Foutput384, Foutput512, Foutput512_224,
       Foutput512_256, FoutputRipeMD128, FoutputRipeMD160, FoutputTiger,
+      FoutputKck224, FoutputKck256, FoutputKck288, FoutputKck384, FoutputKck512,
       FoutputSha3_224, FoutputSha3_256, FoutputSha3_384, FoutputSha3_512,
       FoutputGost2012_256, FoutputGost2012_512: TBytes;
 
@@ -187,6 +188,17 @@ begin
   FoutputTiger := THex.Decode
     ('1d7a658c75f8f004916e7b07e2a2e10aec7de2ae124d3647');
 
+  FoutputKck224 := THex.Decode
+    ('b73d595a2ba9af815e9f2b4e53e78581ebd34a80b3bbaac4e702c4cc');
+  FoutputKck256 := THex.Decode
+    ('9663d10c73ee294054dc9faf95647cb99731d12210ff7075fb3d3395abfb9821');
+  FoutputKck288 := THex.Decode
+    ('36145df8742160a1811139494d708f9a12757c30dedc622a98aa6ecb69da32a34ea55441');
+  FoutputKck384 := THex.Decode
+    ('892dfdf5d51e4679bf320cd16d4c9dc6f749744608e003add7fba894acff87361efa4e5799be06b6461f43b60ae97048');
+  FoutputKck512 := THex.Decode
+    ('8852c63be8cfc21541a4ee5e5a9a852fc2f7a9adec2ff3a13718ab4ed81aaea0b87b7eb397323548e261a64e7fc75198f6663a11b22cd957f7c8ec858a1c7755');
+
   FoutputSha3_224 := THex.Decode
     ('3b16546bbc7be2706a031dcafd56373d9884367641d8c59af3c860f7');
   FoutputSha3_256 := THex.Decode
@@ -229,6 +241,12 @@ begin
 
   DoTestHMac('HMac-TIGER', FoutputTiger);
 
+  DoTestHMac('HMac-KECCAK224', 224, FoutputKck224);
+  DoTestHMac('HMac-KECCAK256', 256, FoutputKck256);
+  DoTestHMac('HMac-KECCAK288', 288, FoutputKck288);
+  DoTestHMac('HMac-KECCAK384', 384, FoutputKck384);
+  DoTestHMac('HMac-KECCAK512', 512, FoutputKck512);
+
   DoTestHMac('HMac-SHA3-224', 224, FoutputSha3_224);
   DoTestHMac('HMac-SHA3-256', 256, FoutputSha3_256);
   DoTestHMac('HMac-SHA3-384', 384, FoutputSha3_384);
@@ -252,6 +270,12 @@ begin
   DoTestHMac('HMac/RIPEMD160', FoutputRipeMD160);
   DoTestHMac('HMac/TIGER', FoutputTiger);
 
+  DoTestHMac('HMac/KECCAK224', 224, FoutputKck224);
+  DoTestHMac('HMac/KECCAK256', 256, FoutputKck256);
+  DoTestHMac('HMac/KECCAK288', 288, FoutputKck288);
+  DoTestHMac('HMac/KECCAK384', 384, FoutputKck384);
+  DoTestHMac('HMac/KECCAK512', 512, FoutputKck512);
+
   DoTestHMac('HMac/SHA3-224', 224, FoutputSha3_224);
   DoTestHMac('HMac/SHA3-256', 256, FoutputSha3_256);
   DoTestHMac('HMac/SHA3-384', 384, FoutputSha3_384);

+ 25 - 10
CryptoLib/src/Asn1/Nist/ClpNistObjectIdentifiers.pas

@@ -35,15 +35,15 @@ type
     FIsBooted: Boolean;
     FNistAlgorithm, FHashAlgs, FSigAlgs, FIdSha256, FIdSha384, FIdSha512,
       FIdSha224, FIdSha512_224, FIdSha512_256, FIdSha3_224, FIdSha3_256,
-      FIdSha3_384, FIdSha3_512, FIdHMacWithSha3_224, FIdHMacWithSha3_256,
-      FIdHMacWithSha3_384, FIdHMacWithSha3_512, FAES, FIdAES128Ecb,
-      FIdAes128Cbc, FIdAes128Ofb, FIdAes128Cfb, FidAes192Ecb, FIdAes192Cbc,
-      FIdAes192Ofb, FIdAes192Cfb, FIdAes256Ecb, FIdAes256Cbc, FIdAes256Ofb,
-      FIdAes256Cfb, FIdDsaWithSha2, FDsaWithSha224, FDsaWithSha256,
-      FDsaWithSha384, FDsaWithSha512, FIdDsaWithSha3_224, FIdDsaWithSha3_256,
-      FIdDsaWithSha3_384, FIdDsaWithSha3_512, FIdECDsaWithSha3_224,
-      FIdECDsaWithSha3_256, FIdECDsaWithSha3_384, FIdECDsaWithSha3_512
-      : IDerObjectIdentifier;
+      FIdSha3_384, FIdSha3_512, FIdShake128, FIdShake256, FIdHMacWithSha3_224,
+      FIdHMacWithSha3_256, FIdHMacWithSha3_384, FIdHMacWithSha3_512, FAES,
+      FIdAES128Ecb, FIdAes128Cbc, FIdAes128Ofb, FIdAes128Cfb, FidAes192Ecb,
+      FIdAes192Cbc, FIdAes192Ofb, FIdAes192Cfb, FIdAes256Ecb, FIdAes256Cbc,
+      FIdAes256Ofb, FIdAes256Cfb, FIdDsaWithSha2, FDsaWithSha224,
+      FDsaWithSha256, FDsaWithSha384, FDsaWithSha512, FIdDsaWithSha3_224,
+      FIdDsaWithSha3_256, FIdDsaWithSha3_384, FIdDsaWithSha3_512,
+      FIdECDsaWithSha3_224, FIdECDsaWithSha3_256, FIdECDsaWithSha3_384,
+      FIdECDsaWithSha3_512: IDerObjectIdentifier;
 
     class function GetNistAlgorithm: IDerObjectIdentifier; static; inline;
     class function GetHashAlgs: IDerObjectIdentifier; static; inline;
@@ -55,6 +55,8 @@ type
     class function GetIdSha3_256: IDerObjectIdentifier; static; inline;
     class function GetIdSha3_384: IDerObjectIdentifier; static; inline;
     class function GetIdSha3_512: IDerObjectIdentifier; static; inline;
+    class function GetIdShake128: IDerObjectIdentifier; static; inline;
+    class function GetIdShake256: IDerObjectIdentifier; static; inline;
     class function GetIdSha384: IDerObjectIdentifier; static; inline;
     class function GetIdSha512: IDerObjectIdentifier; static; inline;
     class function GetIdSha512_224: IDerObjectIdentifier; static; inline;
@@ -121,6 +123,8 @@ type
     class property IdSha3_256: IDerObjectIdentifier read GetIdSha3_256;
     class property IdSha3_384: IDerObjectIdentifier read GetIdSha3_384;
     class property IdSha3_512: IDerObjectIdentifier read GetIdSha3_512;
+    class property IdShake128: IDerObjectIdentifier read GetIdShake128;
+    class property IdShake256: IDerObjectIdentifier read GetIdShake256;
 
     class property IdHMacWithSha3_224: IDerObjectIdentifier
       read GetIdHMacWithSha3_224;
@@ -332,6 +336,16 @@ begin
   result := FIdSha3_512;
 end;
 
+class function TNistObjectIdentifiers.GetIdShake128: IDerObjectIdentifier;
+begin
+  result := FIdShake128;
+end;
+
+class function TNistObjectIdentifiers.GetIdShake256: IDerObjectIdentifier;
+begin
+  result := FIdShake256;
+end;
+
 class function TNistObjectIdentifiers.GetIdSha512: IDerObjectIdentifier;
 begin
   result := FIdSha512;
@@ -427,7 +441,8 @@ begin
     FIdSha3_256 := HashAlgs.Branch('8');
     FIdSha3_384 := HashAlgs.Branch('9');
     FIdSha3_512 := HashAlgs.Branch('10');
-
+    FIdShake128 := HashAlgs.Branch('11');
+    FIdShake256 := HashAlgs.Branch('12');
     FIdHMacWithSha3_224 := HashAlgs.Branch('13');
     FIdHMacWithSha3_256 := HashAlgs.Branch('14');
     FIdHMacWithSha3_384 := HashAlgs.Branch('15');

+ 44 - 36
CryptoLib/src/Security/ClpDigestUtilities.pas

@@ -55,10 +55,11 @@ type
 {$SCOPEDENUMS ON}
     TDigestAlgorithm = (BLAKE2B_160, BLAKE2B_256, BLAKE2B_384, BLAKE2B_512,
       BLAKE2S_128, BLAKE2S_160, BLAKE2S_224, BLAKE2S_256, GOST3411,
-      GOST3411_2012_256, GOST3411_2012_512, MD2, MD4, MD5, NONE, RIPEMD128,
-      RIPEMD160, RIPEMD256, RIPEMD320, SHA_1, SHA_224, SHA_256, SHA_384,
-      SHA_512, SHA_512_224, SHA_512_256, SHA3_224, SHA3_256, SHA3_384, SHA3_512,
-      TIGER, WHIRLPOOL);
+      GOST3411_2012_256, GOST3411_2012_512, KECCAK_224, KECCAK_256, KECCAK_288,
+      KECCAK_384, KECCAK_512, MD2, MD4, MD5, NONE, RIPEMD128, RIPEMD160,
+      RIPEMD256, RIPEMD320, SHA_1, SHA_224, SHA_256, SHA_384, SHA_512,
+      SHA_512_224, SHA_512_256, SHA3_224, SHA3_256, SHA3_384, SHA3_512, TIGER,
+      WHIRLPOOL);
 {$SCOPEDENUMS OFF}
   class procedure Boot(); static;
   class constructor CreateDigestUtilities();
@@ -132,63 +133,54 @@ begin
     TDigestAlgorithm.BLAKE2B_160:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateBlake2B_160);
-
         Exit;
       end;
 
     TDigestAlgorithm.BLAKE2B_256:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateBlake2B_256);
-
         Exit;
       end;
 
     TDigestAlgorithm.BLAKE2B_384:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateBlake2B_384);
-
         Exit;
       end;
 
     TDigestAlgorithm.BLAKE2B_512:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateBlake2B_512);
-
         Exit;
       end;
 
     TDigestAlgorithm.BLAKE2S_128:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateBlake2S_128);
-
         Exit;
       end;
 
     TDigestAlgorithm.BLAKE2S_160:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateBlake2S_160);
-
         Exit;
       end;
 
     TDigestAlgorithm.BLAKE2S_224:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateBlake2S_224);
-
         Exit;
       end;
 
     TDigestAlgorithm.BLAKE2S_256:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateBlake2S_256);
-
         Exit;
       end;
 
     TDigestAlgorithm.GOST3411:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateGost());
-
         Exit;
       end;
 
@@ -196,7 +188,6 @@ begin
       begin
         result := TDigest.Create
           (THashFactory.TCrypto.CreateGOST3411_2012_256());
-
         Exit;
       end;
 
@@ -204,28 +195,54 @@ begin
       begin
         result := TDigest.Create
           (THashFactory.TCrypto.CreateGOST3411_2012_512());
+        Exit;
+      end;
+
+    TDigestAlgorithm.KECCAK_224:
+      begin
+        result := TDigest.Create(THashFactory.TCrypto.CreateKeccak_224());
+        Exit;
+      end;
+
+    TDigestAlgorithm.KECCAK_256:
+      begin
+        result := TDigest.Create(THashFactory.TCrypto.CreateKeccak_256());
+        Exit;
+      end;
+
+    TDigestAlgorithm.KECCAK_288:
+      begin
+        result := TDigest.Create(THashFactory.TCrypto.CreateKeccak_288());
+        Exit;
+      end;
+
+    TDigestAlgorithm.KECCAK_384:
+      begin
+        result := TDigest.Create(THashFactory.TCrypto.CreateKeccak_384());
+        Exit;
+      end;
 
+    TDigestAlgorithm.KECCAK_512:
+      begin
+        result := TDigest.Create(THashFactory.TCrypto.CreateKeccak_512());
         Exit;
       end;
 
     TDigestAlgorithm.MD2:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateMD2());
-
         Exit;
       end;
 
     TDigestAlgorithm.MD4:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateMD4());
-
         Exit;
       end;
 
     TDigestAlgorithm.MD5:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateMD5());
-
         Exit;
       end;
 
@@ -233,127 +250,108 @@ begin
       begin
         result := TDigest.Create
           (THashFactory.TNullDigestFactory.CreateNullDigest());
-
         Exit;
       end;
 
     TDigestAlgorithm.RIPEMD128:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateRIPEMD128());
-
         Exit;
       end;
 
     TDigestAlgorithm.RIPEMD160:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateRIPEMD160());
-
         Exit;
       end;
 
     TDigestAlgorithm.RIPEMD256:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateRIPEMD256());
-
         Exit;
       end;
 
     TDigestAlgorithm.RIPEMD320:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateRIPEMD320());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA_1:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA1());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA_224:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA2_224());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA_256:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA2_256());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA_384:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA2_384());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA_512:
       begin
-
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA2_512());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA_512_224:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA2_512_224());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA_512_256:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA2_512_256());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA3_224:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA3_224());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA3_256:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA3_256());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA3_384:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA3_384());
-
         Exit;
       end;
 
     TDigestAlgorithm.SHA3_512:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateSHA3_512());
-
         Exit;
       end;
 
     TDigestAlgorithm.TIGER:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateTiger_3_192);
-
         Exit;
       end;
 
     TDigestAlgorithm.WHIRLPOOL:
       begin
         result := TDigest.Create(THashFactory.TCrypto.CreateWhirlPool);
-
         Exit;
       end
   else
@@ -416,10 +414,18 @@ begin
 
     Falgorithms.Add(TCryptoProObjectIdentifiers.GostR3411.id, 'GOST3411');
 
+    Falgorithms.Add('KECCAK224', 'KECCAK-224');
+    Falgorithms.Add('KECCAK256', 'KECCAK-256');
+    Falgorithms.Add('KECCAK288', 'KECCAK-288');
+    Falgorithms.Add('KECCAK384', 'KECCAK-384');
+    Falgorithms.Add('KECCAK512', 'KECCAK-512');
+
     Falgorithms.Add(TNistObjectIdentifiers.IdSha3_224.id, 'SHA3-224');
     Falgorithms.Add(TNistObjectIdentifiers.IdSha3_256.id, 'SHA3-256');
     Falgorithms.Add(TNistObjectIdentifiers.IdSha3_384.id, 'SHA3-384');
     Falgorithms.Add(TNistObjectIdentifiers.IdSha3_512.id, 'SHA3-512');
+    Falgorithms.Add(TNistObjectIdentifiers.IdShake128.id, 'SHAKE128');
+    Falgorithms.Add(TNistObjectIdentifiers.IdShake256.id, 'SHAKE256');
 
     TMiscObjectIdentifiers.Boot;
 
@@ -453,6 +459,8 @@ begin
     Foids.Add('SHA3-256', TNistObjectIdentifiers.IdSha3_256);
     Foids.Add('SHA3-384', TNistObjectIdentifiers.IdSha3_384);
     Foids.Add('SHA3-512', TNistObjectIdentifiers.IdSha3_512);
+    Foids.Add('SHAKE128', TNistObjectIdentifiers.IdShake128);
+    Foids.Add('SHAKE256', TNistObjectIdentifiers.IdShake256);
     Foids.Add('RIPEMD128', TTeleTrusTObjectIdentifiers.RIPEMD128);
     Foids.Add('RIPEMD160', TTeleTrusTObjectIdentifiers.RIPEMD160);
     Foids.Add('RIPEMD256', TTeleTrusTObjectIdentifiers.RIPEMD256);

+ 12 - 4
CryptoLib/src/Security/ClpGeneratorUtilities.pas

@@ -225,6 +225,12 @@ begin
   AddHMacKeyGenerator('SHA512/224', []);
   AddHMacKeyGenerator('SHA512/256', []);
 
+  AddHMacKeyGenerator('KECCAK224', []);
+  AddHMacKeyGenerator('KECCAK256', []);
+  AddHMacKeyGenerator('KECCAK288', []);
+  AddHMacKeyGenerator('KECCAK384', []);
+  AddHMacKeyGenerator('KECCAK512', []);
+
   AddHMacKeyGenerator('SHA3-224',
     [TNistObjectIdentifiers.IdHMacWithSha3_224.ID]);
   AddHMacKeyGenerator('SHA3-256',
@@ -264,13 +270,15 @@ begin
     'HMACMD5', 'HMACRIPEMD128', 'SALSA20']);
   AddDefaultKeySizeEntries(160, ['HMACRIPEMD160', 'HMACSHA1']);
   AddDefaultKeySizeEntries(192, ['AES', 'AES192', 'HMACTIGER']);
-  AddDefaultKeySizeEntries(224, ['HMACSHA3-224', 'HMACSHA224',
+  AddDefaultKeySizeEntries(224, ['HMACSHA3-224', 'HMACKECCAK224', 'HMACSHA224',
     'HMACSHA512/224']);
   AddDefaultKeySizeEntries(256, ['AES256', 'HMACGOST3411-2012-256',
-    'HMACSHA3-256', 'HMACSHA256', 'HMACSHA512/256']);
-  AddDefaultKeySizeEntries(384, ['HMACSHA3-384', 'HMACSHA384']);
+    'HMACSHA3-256', 'HMACKECCAK256', 'HMACSHA256', 'HMACSHA512/256']);
+  AddDefaultKeySizeEntries(288, ['HMACKECCAK288']);
+  AddDefaultKeySizeEntries(384, ['HMACSHA3-384', 'HMACKECCAK384',
+    'HMACSHA384']);
   AddDefaultKeySizeEntries(512, ['HMACGOST3411-2012-512', 'HMACSHA3-512',
-    'HMACSHA512']);
+    'HMACKECCAK512', 'HMACSHA512']);
 end;
 
 class constructor TGeneratorUtilities.CreateGeneratorUtilities;