package org.bouncycastle.crypto.generators; import java.math.BigInteger; import java.security.SecureRandom; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator; import org.bouncycastle.crypto.KeyGenerationParameters; import org.bouncycastle.crypto.params.AsymmetricKeyParameter; import org.bouncycastle.crypto.params.DSAKeyGenerationParameters; import org.bouncycastle.crypto.params.DSAParameters; import org.bouncycastle.crypto.params.DSAPrivateKeyParameters; import org.bouncycastle.crypto.params.DSAPublicKeyParameters; import org.bouncycastle.math.ec.WNafUtil; import org.bouncycastle.util.BigIntegers; /* loaded from: classes6.dex */ public class DSAKeyPairGenerator implements AsymmetricCipherKeyPairGenerator { private static final BigInteger ONE = BigInteger.valueOf(1); private DSAKeyGenerationParameters param; @Override // org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator public void init(KeyGenerationParameters keyGenerationParameters) { this.param = (DSAKeyGenerationParameters) keyGenerationParameters; } @Override // org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator public AsymmetricCipherKeyPair generateKeyPair() { DSAParameters parameters = this.param.getParameters(); BigInteger generatePrivateKey = generatePrivateKey(parameters.getQ(), this.param.getRandom()); return new AsymmetricCipherKeyPair((AsymmetricKeyParameter) new DSAPublicKeyParameters(calculatePublicKey(parameters.getP(), parameters.getG(), generatePrivateKey), parameters), (AsymmetricKeyParameter) new DSAPrivateKeyParameters(generatePrivateKey, parameters)); } private static BigInteger generatePrivateKey(BigInteger bigInteger, SecureRandom secureRandom) { BigInteger createRandomInRange; int bitLength = bigInteger.bitLength(); do { BigInteger bigInteger2 = ONE; createRandomInRange = BigIntegers.createRandomInRange(bigInteger2, bigInteger.subtract(bigInteger2), secureRandom); } while (WNafUtil.getNafWeight(createRandomInRange) < (bitLength >>> 2)); return createRandomInRange; } private static BigInteger calculatePublicKey(BigInteger bigInteger, BigInteger bigInteger2, BigInteger bigInteger3) { return bigInteger2.modPow(bigInteger3, bigInteger); } }