From 312ffa7434f9ec8d81d05a8e2b73f37ec823fdc1 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 31 May 2023 19:48:18 +0700 Subject: Refactoring in DH key pair generators - obsolete DHBasicKeyPairGenerator --- .../crypto/generators/DHBasicKeyPairGenerator.cs | 16 ++++++------- .../src/crypto/generators/DHKeyGeneratorHelper.cs | 28 ++++------------------ crypto/src/crypto/generators/DHKeyPairGenerator.cs | 15 ++++++------ .../crypto/generators/ElGamalKeyPairGenerator.cs | 16 ++++++------- crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs | 2 +- 5 files changed, 27 insertions(+), 50 deletions(-) (limited to 'crypto') 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".

*/ + // 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(); } -- cgit 1.4.1