summary refs log tree commit diff
path: root/Crypto/src/crypto/generators/DHKeyGeneratorHelper.cs
diff options
context:
space:
mode:
authorOren Novotny <oren@novotny.org>2014-02-26 10:08:50 -0500
committerOren Novotny <oren@novotny.org>2014-02-26 10:08:50 -0500
commit176743ab5faec2dd275b5efd3a2dd62c610f237a (patch)
tree1d2e50c534a479d749c266d7c52434d8f17f86aa /Crypto/src/crypto/generators/DHKeyGeneratorHelper.cs
parentAdd git files (diff)
downloadBouncyCastle.NET-ed25519-1.7.0.tar.xz
Add BouncyCastle PCL files v1.7.0
Diffstat (limited to 'Crypto/src/crypto/generators/DHKeyGeneratorHelper.cs')
-rw-r--r--Crypto/src/crypto/generators/DHKeyGeneratorHelper.cs53
1 files changed, 53 insertions, 0 deletions
diff --git a/Crypto/src/crypto/generators/DHKeyGeneratorHelper.cs b/Crypto/src/crypto/generators/DHKeyGeneratorHelper.cs
new file mode 100644
index 000000000..756e8482a
--- /dev/null
+++ b/Crypto/src/crypto/generators/DHKeyGeneratorHelper.cs
@@ -0,0 +1,53 @@
+using System;
+
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Math;
+using Org.BouncyCastle.Security;
+using Org.BouncyCastle.Utilities;
+
+namespace Org.BouncyCastle.Crypto.Generators
+{
+	class DHKeyGeneratorHelper
+	{
+		internal static readonly DHKeyGeneratorHelper Instance = new DHKeyGeneratorHelper();
+
+		private DHKeyGeneratorHelper()
+		{
+		}
+
+		internal BigInteger CalculatePrivate(
+			DHParameters	dhParams,
+			SecureRandom	random)
+		{
+			int limit = dhParams.L;
+
+			if (limit != 0)
+			{
+				return new BigInteger(limit, random).SetBit(limit - 1);
+			}
+
+			BigInteger min = BigInteger.Two;
+			int m = dhParams.M;
+			if (m != 0)
+			{
+				min = BigInteger.One.ShiftLeft(m - 1);
+			}
+
+			BigInteger max = dhParams.P.Subtract(BigInteger.Two);
+			BigInteger q = dhParams.Q;
+			if (q != null)
+			{
+				max = q.Subtract(BigInteger.Two);
+			}
+
+			return BigIntegers.CreateRandomInRange(min, max, random);
+		}
+
+		internal BigInteger CalculatePublic(
+			DHParameters	dhParams,
+			BigInteger		x)
+		{
+			return dhParams.G.ModPow(x, dhParams.P);
+		}
+	}
+}