summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-05-31 19:48:18 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-05-31 19:48:18 +0700
commit312ffa7434f9ec8d81d05a8e2b73f37ec823fdc1 (patch)
tree9c9ad7d1c4d5811eb1f29d8f21f65900255cf288
parentTODO for stricter HC-256 key/IV (diff)
downloadBouncyCastle.NET-ed25519-312ffa7434f9ec8d81d05a8e2b73f37ec823fdc1.tar.xz
Refactoring in DH key pair generators
- obsolete DHBasicKeyPairGenerator
-rw-r--r--crypto/src/crypto/generators/DHBasicKeyPairGenerator.cs16
-rw-r--r--crypto/src/crypto/generators/DHKeyGeneratorHelper.cs28
-rw-r--r--crypto/src/crypto/generators/DHKeyPairGenerator.cs15
-rw-r--r--crypto/src/crypto/generators/ElGamalKeyPairGenerator.cs16
-rw-r--r--crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs2
5 files changed, 27 insertions, 50 deletions
diff --git a/crypto/src/crypto/generators/DHBasicKeyPairGenerator.cs b/crypto/src/crypto/generators/DHBasicKeyPairGenerator.cs
index 51b3af687..7842df683 100644
--- a/crypto/src/crypto/generators/DHBasicKeyPairGenerator.cs
+++ b/crypto/src/crypto/generators/DHBasicKeyPairGenerator.cs
@@ -11,24 +11,24 @@ namespace Org.BouncyCastle.Crypto.Generators
      * This generates keys consistent for use with the basic algorithm for
      * Diffie-Hellman.
      */
+    // TODO[api] sealed
+    [Obsolete("Use 'DHKeyPairGenerator' instead")]
     public class DHBasicKeyPairGenerator
 		: IAsymmetricCipherKeyPairGenerator
     {
-        private DHKeyGenerationParameters param;
+        private DHKeyGenerationParameters m_parameters;
 
-        public virtual void Init(
-			KeyGenerationParameters parameters)
+        public virtual void Init(KeyGenerationParameters parameters)
         {
-            this.param = (DHKeyGenerationParameters)parameters;
+            m_parameters = (DHKeyGenerationParameters)parameters;
         }
 
         public virtual AsymmetricCipherKeyPair GenerateKeyPair()
         {
-			DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.Instance;
-			DHParameters dhp = param.Parameters;
+			DHParameters dhp = m_parameters.Parameters;
 
-			BigInteger x = helper.CalculatePrivate(dhp, param.Random);
-			BigInteger y = helper.CalculatePublic(dhp, x);
+			BigInteger x = DHKeyGeneratorHelper.CalculatePrivate(dhp, m_parameters.Random);
+			BigInteger y = DHKeyGeneratorHelper.CalculatePublic(dhp, x);
 
 			return new AsymmetricCipherKeyPair(
                 new DHPublicKeyParameters(y, dhp),
diff --git a/crypto/src/crypto/generators/DHKeyGeneratorHelper.cs b/crypto/src/crypto/generators/DHKeyGeneratorHelper.cs
index 68aba64f7..e5f1fbaf1 100644
--- a/crypto/src/crypto/generators/DHKeyGeneratorHelper.cs
+++ b/crypto/src/crypto/generators/DHKeyGeneratorHelper.cs
@@ -1,5 +1,3 @@
-using System;
-
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Math.EC.Multiplier;
@@ -8,17 +6,9 @@ using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Crypto.Generators
 {
-    class DHKeyGeneratorHelper
+    internal static class DHKeyGeneratorHelper
     {
-        internal static readonly DHKeyGeneratorHelper Instance = new DHKeyGeneratorHelper();
-
-        private DHKeyGeneratorHelper()
-        {
-        }
-
-        internal BigInteger CalculatePrivate(
-            DHParameters	dhParams,
-            SecureRandom	random)
+        internal static BigInteger CalculatePrivate(DHParameters dhParams, SecureRandom	random)
         {
             int limit = dhParams.L;
 
@@ -29,9 +19,7 @@ namespace Org.BouncyCastle.Crypto.Generators
                 {
                     BigInteger x = new BigInteger(limit, random).SetBit(limit - 1);
                     if (WNafUtilities.GetNafWeight(x) >= minWeight)
-                    {
                         return x;
-                    }
                 }
             }
 
@@ -42,11 +30,7 @@ namespace Org.BouncyCastle.Crypto.Generators
                 min = BigInteger.One.ShiftLeft(m - 1);
             }
 
-            BigInteger q = dhParams.Q;
-            if (q == null)
-            {
-                q = dhParams.P;
-            }
+            BigInteger q = dhParams.Q ?? dhParams.P;
             BigInteger max = q.Subtract(BigInteger.Two);
 
             {
@@ -55,16 +39,12 @@ namespace Org.BouncyCastle.Crypto.Generators
                 {
                     BigInteger x = BigIntegers.CreateRandomInRange(min, max, random);
                     if (WNafUtilities.GetNafWeight(x) >= minWeight)
-                    {
                         return x;
-                    }
                 }
             }
         }
 
-        internal BigInteger CalculatePublic(
-            DHParameters	dhParams,
-            BigInteger		x)
+        internal static BigInteger CalculatePublic(DHParameters	dhParams, BigInteger x)
         {
             return dhParams.G.ModPow(x, dhParams.P);
         }
diff --git a/crypto/src/crypto/generators/DHKeyPairGenerator.cs b/crypto/src/crypto/generators/DHKeyPairGenerator.cs
index 3bf58ba1b..171d86f92 100644
--- a/crypto/src/crypto/generators/DHKeyPairGenerator.cs
+++ b/crypto/src/crypto/generators/DHKeyPairGenerator.cs
@@ -11,24 +11,23 @@ namespace Org.BouncyCastle.Crypto.Generators
      * This generates keys consistent for use in the MTI/A0 key agreement protocol
      * as described in "Handbook of Applied Cryptography", Pages 516-519.
      */
+    // TODO[api] sealed
     public class DHKeyPairGenerator
 		: IAsymmetricCipherKeyPairGenerator
     {
-		private DHKeyGenerationParameters param;
+		private DHKeyGenerationParameters m_parameters;
 
-		public virtual void Init(
-			KeyGenerationParameters parameters)
+		public virtual void Init(KeyGenerationParameters parameters)
         {
-            this.param = (DHKeyGenerationParameters)parameters;
+            m_parameters = (DHKeyGenerationParameters)parameters;
         }
 
 		public virtual AsymmetricCipherKeyPair GenerateKeyPair()
         {
-			DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.Instance;
-			DHParameters dhp = param.Parameters;
+			DHParameters dhp = m_parameters.Parameters;
 
-			BigInteger x = helper.CalculatePrivate(dhp, param.Random);
-			BigInteger y = helper.CalculatePublic(dhp, x);
+			BigInteger x = DHKeyGeneratorHelper.CalculatePrivate(dhp, m_parameters.Random);
+			BigInteger y = DHKeyGeneratorHelper.CalculatePublic(dhp, x);
 
 			return new AsymmetricCipherKeyPair(
                 new DHPublicKeyParameters(y, dhp),
diff --git a/crypto/src/crypto/generators/ElGamalKeyPairGenerator.cs b/crypto/src/crypto/generators/ElGamalKeyPairGenerator.cs
index 227e7fe94..790e3dacf 100644
--- a/crypto/src/crypto/generators/ElGamalKeyPairGenerator.cs
+++ b/crypto/src/crypto/generators/ElGamalKeyPairGenerator.cs
@@ -11,30 +11,28 @@ namespace Org.BouncyCastle.Crypto.Generators
      * This Generates keys consistent for use with ElGamal as described in
      * page 164 of "Handbook of Applied Cryptography".</p>
      */
+    // TODO[api] sealed
     public class ElGamalKeyPairGenerator
 		: IAsymmetricCipherKeyPairGenerator
     {
-        private ElGamalKeyGenerationParameters param;
+        private ElGamalKeyGenerationParameters m_parameters;
 
-        public void Init(
-			KeyGenerationParameters parameters)
+        public void Init(KeyGenerationParameters parameters)
         {
-            this.param = (ElGamalKeyGenerationParameters) parameters;
+            m_parameters = (ElGamalKeyGenerationParameters)parameters;
         }
 
         public AsymmetricCipherKeyPair GenerateKeyPair()
         {
-			DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.Instance;
-			ElGamalParameters egp = param.Parameters;
+			ElGamalParameters egp = m_parameters.Parameters;
 			DHParameters dhp = new DHParameters(egp.P, egp.G, null, 0, egp.L);
 
-			BigInteger x = helper.CalculatePrivate(dhp, param.Random);
-			BigInteger y = helper.CalculatePublic(dhp, x);
+			BigInteger x = DHKeyGeneratorHelper.CalculatePrivate(dhp, m_parameters.Random);
+			BigInteger y = DHKeyGeneratorHelper.CalculatePublic(dhp, x);
 
 			return new AsymmetricCipherKeyPair(
                 new ElGamalPublicKeyParameters(y, egp),
                 new ElGamalPrivateKeyParameters(x, egp));
         }
     }
-
 }
diff --git a/crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs b/crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs
index 6a947c23b..7d71fe9d6 100644
--- a/crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs
+++ b/crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs
@@ -109,7 +109,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC
 
         public virtual AsymmetricCipherKeyPair GenerateKeyPair()
         {
-            DHBasicKeyPairGenerator keyPairGenerator = new DHBasicKeyPairGenerator();
+            DHKeyPairGenerator keyPairGenerator = new DHKeyPairGenerator();
             keyPairGenerator.Init(new DHKeyGenerationParameters(m_crypto.SecureRandom, m_domainParameters));
             return keyPairGenerator.GenerateKeyPair();
         }