summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-03-04 18:01:41 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-03-04 18:01:41 +0700
commitca8d87e9cdf1ae70d1f2889f6e939d6be7afaf53 (patch)
treef6f4e6f3fd597976f4cabb49e398f3690114eea7
parentRefactor VMPC classes (diff)
downloadBouncyCastle.NET-ed25519-ca8d87e9cdf1ae70d1f2889f6e939d6be7afaf53.tar.xz
Refactoring in CipherFactory
-rw-r--r--crypto/src/crypto/util/CipherFactory.cs102
1 files changed, 53 insertions, 49 deletions
diff --git a/crypto/src/crypto/util/CipherFactory.cs b/crypto/src/crypto/util/CipherFactory.cs
index 56f57c545..725159bfd 100644
--- a/crypto/src/crypto/util/CipherFactory.cs
+++ b/crypto/src/crypto/util/CipherFactory.cs
@@ -15,13 +15,14 @@ using Org.BouncyCastle.Crypto.Parameters;
 
 namespace Org.BouncyCastle.Crypto.Utilities
 {
+    // TODO[api] Make static
     public class CipherFactory
     {
         private CipherFactory()
         {
         }
 
-        private static readonly short[] rc2Ekb =
+        private static readonly byte[] RC2Ekb =
         {
             0x5d, 0xbe, 0x9b, 0x8b, 0x11, 0x99, 0x6e, 0x4d, 0x59, 0xf3, 0x85, 0xa6, 0x3f, 0xb7, 0x83, 0xc5,
             0xe4, 0x73, 0x6b, 0x3a, 0x68, 0x5a, 0xc0, 0x47, 0xa0, 0x64, 0x34, 0x0c, 0xf1, 0xd0, 0x52, 0xa5,
@@ -48,64 +49,67 @@ namespace Org.BouncyCastle.Crypto.Utilities
 
             if (encAlg.Equals(PkcsObjectIdentifiers.rc4))
             {
-                IStreamCipher cipher = new RC4Engine();
-                cipher.Init(forEncryption, encKey);
-                return cipher;
+                var rc4Engine = new RC4Engine();
+                rc4Engine.Init(forEncryption, encKey);
+                return rc4Engine;
             }
-            else
-            {
-                BufferedBlockCipher cipher = CreateCipher(encryptionAlgID.Algorithm);
-                Asn1Object sParams = encryptionAlgID.Parameters.ToAsn1Object();
 
-                if (sParams != null && !(sParams is DerNull))
+            BufferedBlockCipher cipher = CreateCipher(encryptionAlgID.Algorithm);
+            Asn1Object sParams = encryptionAlgID.Parameters.ToAsn1Object();
+
+            if (sParams != null && !(sParams is DerNull))
+            {
+                if (encAlg.Equals(PkcsObjectIdentifiers.DesEde3Cbc)
+                    || encAlg.Equals(AlgorithmIdentifierFactory.IDEA_CBC)
+                    || encAlg.Equals(NistObjectIdentifiers.IdAes128Cbc)
+                    || encAlg.Equals(NistObjectIdentifiers.IdAes192Cbc)
+                    || encAlg.Equals(NistObjectIdentifiers.IdAes256Cbc)
+                    || encAlg.Equals(NttObjectIdentifiers.IdCamellia128Cbc)
+                    || encAlg.Equals(NttObjectIdentifiers.IdCamellia192Cbc)
+                    || encAlg.Equals(NttObjectIdentifiers.IdCamellia256Cbc)
+                    || encAlg.Equals(KisaObjectIdentifiers.IdSeedCbc)
+                    || encAlg.Equals(OiwObjectIdentifiers.DesCbc))
                 {
-                    if (encAlg.Equals(PkcsObjectIdentifiers.DesEde3Cbc)
-                        || encAlg.Equals(AlgorithmIdentifierFactory.IDEA_CBC)
-                        || encAlg.Equals(NistObjectIdentifiers.IdAes128Cbc)
-                        || encAlg.Equals(NistObjectIdentifiers.IdAes192Cbc)
-                        || encAlg.Equals(NistObjectIdentifiers.IdAes256Cbc)
-                        || encAlg.Equals(NttObjectIdentifiers.IdCamellia128Cbc)
-                        || encAlg.Equals(NttObjectIdentifiers.IdCamellia192Cbc)
-                        || encAlg.Equals(NttObjectIdentifiers.IdCamellia256Cbc)
-                        || encAlg.Equals(KisaObjectIdentifiers.IdSeedCbc)
-                        || encAlg.Equals(OiwObjectIdentifiers.DesCbc))
-                    {
-                        cipher.Init(forEncryption, new ParametersWithIV(encKey,
-                            Asn1OctetString.GetInstance(sParams).GetOctets()));
-                    }
-                    else if (encAlg.Equals(AlgorithmIdentifierFactory.CAST5_CBC))
-                    {
-                        Cast5CbcParameters cbcParams = Cast5CbcParameters.GetInstance(sParams);
+                    cipher.Init(forEncryption, new ParametersWithIV(encKey,
+                        Asn1OctetString.GetInstance(sParams).GetOctets()));
+                }
+                else if (encAlg.Equals(AlgorithmIdentifierFactory.CAST5_CBC))
+                {
+                    Cast5CbcParameters cbcParams = Cast5CbcParameters.GetInstance(sParams);
 
-                        cipher.Init(forEncryption, new ParametersWithIV(encKey, cbcParams.GetIV()));
-                    }
-                    else if (encAlg.Equals(PkcsObjectIdentifiers.RC2Cbc))
-                    {
-                        RC2CbcParameter cbcParams = RC2CbcParameter.GetInstance(sParams);
+                    cipher.Init(forEncryption, new ParametersWithIV(encKey, cbcParams.GetIV()));
+                }
+                else if (encAlg.Equals(PkcsObjectIdentifiers.RC2Cbc))
+                {
+                    RC2CbcParameter cbcParams = RC2CbcParameter.GetInstance(sParams);
 
-                        cipher.Init(forEncryption, new ParametersWithIV(new RC2Parameters(((KeyParameter)encKey).GetKey(), rc2Ekb[cbcParams.RC2ParameterVersion.IntValue]), cbcParams.GetIV()));
-                    }
-                    else
-                    {
-                        throw new InvalidOperationException("cannot match parameters");
-                    }
+                    cipher.Init(forEncryption,
+                        new ParametersWithIV(
+                            new RC2Parameters(
+                                ((KeyParameter)encKey).GetKey(),
+                                RC2Ekb[cbcParams.RC2ParameterVersion.IntValue]),
+                        cbcParams.GetIV()));
                 }
                 else
                 {
-                    if (encAlg.Equals(PkcsObjectIdentifiers.DesEde3Cbc)
-                        || encAlg.Equals(AlgorithmIdentifierFactory.IDEA_CBC)
-                        || encAlg.Equals(AlgorithmIdentifierFactory.CAST5_CBC))
-                    {
-                        cipher.Init(forEncryption, new ParametersWithIV(encKey, new byte[8]));
-                    }
-                    else
-                    {
-                        cipher.Init(forEncryption, encKey);
-                    }
+                    throw new InvalidOperationException("cannot match parameters");
                 }
-
-                return cipher;
             }
+            else
+            {
+                if (encAlg.Equals(PkcsObjectIdentifiers.DesEde3Cbc)
+                    || encAlg.Equals(AlgorithmIdentifierFactory.IDEA_CBC)
+                    || encAlg.Equals(AlgorithmIdentifierFactory.CAST5_CBC))
+                {
+                    cipher.Init(forEncryption, new ParametersWithIV(encKey, new byte[8]));
+                }
+                else
+                {
+                    cipher.Init(forEncryption, encKey);
+                }
+            }
+
+            return cipher;
         }
 
         private static BufferedBlockCipher CreateCipher(DerObjectIdentifier algorithm)