Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/java/org/spongycastle/crypto/generators/DHKeyGeneratorHelper.java')
-rw-r--r--core/src/main/java/org/spongycastle/crypto/generators/DHKeyGeneratorHelper.java67
1 files changed, 67 insertions, 0 deletions
diff --git a/core/src/main/java/org/spongycastle/crypto/generators/DHKeyGeneratorHelper.java b/core/src/main/java/org/spongycastle/crypto/generators/DHKeyGeneratorHelper.java
new file mode 100644
index 00000000..6982d222
--- /dev/null
+++ b/core/src/main/java/org/spongycastle/crypto/generators/DHKeyGeneratorHelper.java
@@ -0,0 +1,67 @@
+package org.spongycastle.crypto.generators;
+
+import java.math.BigInteger;
+import java.security.SecureRandom;
+
+import org.spongycastle.crypto.params.DHParameters;
+import org.spongycastle.math.ec.WNafUtil;
+import org.spongycastle.util.BigIntegers;
+
+class DHKeyGeneratorHelper
+{
+ static final DHKeyGeneratorHelper INSTANCE = new DHKeyGeneratorHelper();
+
+ private static final BigInteger ONE = BigInteger.valueOf(1);
+ private static final BigInteger TWO = BigInteger.valueOf(2);
+
+ private DHKeyGeneratorHelper()
+ {
+ }
+
+ BigInteger calculatePrivate(DHParameters dhParams, SecureRandom random)
+ {
+ int limit = dhParams.getL();
+
+ if (limit != 0)
+ {
+ int minWeight = limit >>> 2;
+ for (;;)
+ {
+ BigInteger x = new BigInteger(limit, random).setBit(limit - 1);
+ if (WNafUtil.getNafWeight(x) >= minWeight)
+ {
+ return x;
+ }
+ }
+ }
+
+ BigInteger min = TWO;
+ int m = dhParams.getM();
+ if (m != 0)
+ {
+ min = ONE.shiftLeft(m - 1);
+ }
+
+ BigInteger q = dhParams.getQ();
+ if (q == null)
+ {
+ q = dhParams.getP();
+ }
+ BigInteger max = q.subtract(TWO);
+
+ int minWeight = max.bitLength() >>> 2;
+ for (;;)
+ {
+ BigInteger x = BigIntegers.createRandomInRange(min, max, random);
+ if (WNafUtil.getNafWeight(x) >= minWeight)
+ {
+ return x;
+ }
+ }
+ }
+
+ BigInteger calculatePublic(DHParameters dhParams, BigInteger x)
+ {
+ return dhParams.getG().modPow(x, dhParams.getP());
+ }
+}