package org.bouncycastle.crypto.signers; import java.math.BigInteger; import java.security.SecureRandom; import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.DSA; import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECKeyParameters; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.params.ParametersWithRandom; import org.bouncycastle.math.ec.ECAlgorithms; import org.bouncycastle.math.ec.ECConstants; import org.bouncycastle.math.ec.ECMultiplier; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.FixedPointCombMultiplier; /* loaded from: classes6.dex */ public class ECGOST3410Signer implements DSA { ECKeyParameters key; SecureRandom random; @Override // org.bouncycastle.crypto.DSA public boolean verifySignature(byte[] bArr, BigInteger bigInteger, BigInteger bigInteger2) { int length = bArr.length; byte[] bArr2 = new byte[length]; for (int i = 0; i != length; i++) { bArr2[i] = bArr[(length - 1) - i]; } BigInteger bigInteger3 = new BigInteger(1, bArr2); BigInteger n = this.key.getParameters().getN(); if (bigInteger.compareTo(ECConstants.ONE) < 0 || bigInteger.compareTo(n) >= 0 || bigInteger2.compareTo(ECConstants.ONE) < 0 || bigInteger2.compareTo(n) >= 0) { return false; } BigInteger modInverse = bigInteger3.modInverse(n); ECPoint normalize = ECAlgorithms.sumOfTwoMultiplies(this.key.getParameters().getG(), bigInteger2.multiply(modInverse).mod(n), ((ECPublicKeyParameters) this.key).getQ(), n.subtract(bigInteger).multiply(modInverse).mod(n)).normalize(); if (normalize.isInfinity()) { return false; } return normalize.getAffineXCoord().toBigInteger().mod(n).equals(bigInteger); } @Override // org.bouncycastle.crypto.DSA public void init(boolean z, CipherParameters cipherParameters) { ECKeyParameters eCKeyParameters; if (!z) { eCKeyParameters = (ECPublicKeyParameters) cipherParameters; } else { if (cipherParameters instanceof ParametersWithRandom) { ParametersWithRandom parametersWithRandom = (ParametersWithRandom) cipherParameters; this.random = parametersWithRandom.getRandom(); this.key = (ECPrivateKeyParameters) parametersWithRandom.getParameters(); return; } this.random = new SecureRandom(); eCKeyParameters = (ECPrivateKeyParameters) cipherParameters; } this.key = eCKeyParameters; } @Override // org.bouncycastle.crypto.DSA public BigInteger[] generateSignature(byte[] bArr) { int length = bArr.length; byte[] bArr2 = new byte[length]; for (int i = 0; i != length; i++) { bArr2[i] = bArr[(length - 1) - i]; } BigInteger bigInteger = new BigInteger(1, bArr2); ECDomainParameters parameters = this.key.getParameters(); BigInteger n = parameters.getN(); BigInteger d = ((ECPrivateKeyParameters) this.key).getD(); ECMultiplier createBasePointMultiplier = createBasePointMultiplier(); while (true) { BigInteger bigInteger2 = new BigInteger(n.bitLength(), this.random); if (!bigInteger2.equals(ECConstants.ZERO)) { BigInteger mod = createBasePointMultiplier.multiply(parameters.getG(), bigInteger2).normalize().getAffineXCoord().toBigInteger().mod(n); if (mod.equals(ECConstants.ZERO)) { continue; } else { BigInteger mod2 = bigInteger2.multiply(bigInteger).add(d.multiply(mod)).mod(n); if (!mod2.equals(ECConstants.ZERO)) { return new BigInteger[]{mod, mod2}; } } } } } protected ECMultiplier createBasePointMultiplier() { return new FixedPointCombMultiplier(); } }