com.fitbur.bouncycastle.crypto.generators.DSAKeyPairGenerator Maven / Gradle / Ivy
package com.fitbur.bouncycastle.crypto.generators;
import java.math.BigInteger;
import java.security.SecureRandom;
import com.fitbur.bouncycastle.crypto.AsymmetricCipherKeyPair;
import com.fitbur.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
import com.fitbur.bouncycastle.crypto.KeyGenerationParameters;
import com.fitbur.bouncycastle.crypto.params.DSAKeyGenerationParameters;
import com.fitbur.bouncycastle.crypto.params.DSAParameters;
import com.fitbur.bouncycastle.crypto.params.DSAPrivateKeyParameters;
import com.fitbur.bouncycastle.crypto.params.DSAPublicKeyParameters;
import com.fitbur.bouncycastle.math.ec.WNafUtil;
import com.fitbur.bouncycastle.util.BigIntegers;
/**
* a DSA key pair generator.
*
* This generates DSA keys in line with the method com.fitburscribed
* in FIPS 186-3 B.1 FFC Key Pair Generation.
*/
public class DSAKeyPairGenerator
implements AsymmetricCipherKeyPairGenerator
{
private static final BigInteger ONE = BigInteger.valueOf(1);
private DSAKeyGenerationParameters param;
public void init(
KeyGenerationParameters param)
{
this.param = (DSAKeyGenerationParameters)param;
}
public AsymmetricCipherKeyPair generateKeyPair()
{
DSAParameters dsaParams = param.getParameters();
BigInteger x = generatePrivateKey(dsaParams.getQ(), param.getRandom());
BigInteger y = calculatePublicKey(dsaParams.getP(), dsaParams.getG(), x);
return new AsymmetricCipherKeyPair(
new DSAPublicKeyParameters(y, dsaParams),
new DSAPrivateKeyParameters(x, dsaParams));
}
private static BigInteger generatePrivateKey(BigInteger q, SecureRandom random)
{
// B.1.2 Key Pair Generation by Testing Candidates
int minWeight = q.bitLength() >>> 2;
for (;;)
{
// TODO Prefer this method? (change test cases that used fixed random)
// B.1.1 Key Pair Generation Using Extra Random Bits
// BigInteger x = new BigInteger(q.bitLength() + 64, random).mod(q.subtract(ONE)).add(ONE);
BigInteger x = BigIntegers.createRandomInRange(ONE, q.subtract(ONE), random);
if (WNafUtil.getNafWeight(x) >= minWeight)
{
return x;
}
}
}
private static BigInteger calculatePublicKey(BigInteger p, BigInteger g, BigInteger x)
{
return g.modPow(x, p);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy