package org.bouncycastle.crypto.signers;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ECDSASigner implements org.bouncycastle.math.ec.ECConstants, org.bouncycastle.crypto.DSAExt {
    private final org.bouncycastle.crypto.signers.DSAKCalculator g;
    private org.bouncycastle.crypto.params.ECKeyParameters h;
    private java.security.SecureRandom i;

    public ECDSASigner() {
        this.g = new org.bouncycastle.crypto.signers.RandomDSAKCalculator();
    }

    @Override // org.bouncycastle.crypto.DSA
    public java.math.BigInteger[] a(byte[] bArr) {
        org.bouncycastle.crypto.params.ECDomainParameters eCDomainParametersG = this.h.g();
        java.math.BigInteger bigIntegerE = eCDomainParametersG.e();
        java.math.BigInteger bigIntegerC = c(bigIntegerE, bArr);
        java.math.BigInteger bigIntegerH = ((org.bouncycastle.crypto.params.ECPrivateKeyParameters) this.h).h();
        if (this.g.c()) {
            this.g.d(bigIntegerE, bigIntegerH, bArr);
        } else {
            this.g.a(bigIntegerE, this.i);
        }
        org.bouncycastle.math.ec.ECMultiplier eCMultiplierD = d();
        while (true) {
            java.math.BigInteger bigIntegerB = this.g.b();
            java.math.BigInteger bigIntegerMod = eCMultiplierD.a(eCDomainParametersG.b(), bigIntegerB).A().f().v().mod(bigIntegerE);
            java.math.BigInteger bigInteger = org.bouncycastle.math.ec.ECConstants.a;
            if (!bigIntegerMod.equals(bigInteger)) {
                java.math.BigInteger bigIntegerMod2 = org.bouncycastle.util.BigIntegers.n(bigIntegerE, bigIntegerB).multiply(bigIntegerC.add(bigIntegerH.multiply(bigIntegerMod))).mod(bigIntegerE);
                if (!bigIntegerMod2.equals(bigInteger)) {
                    return new java.math.BigInteger[]{bigIntegerMod, bigIntegerMod2};
                }
            }
        }
    }

    @Override // org.bouncycastle.crypto.DSA
    public boolean b(byte[] bArr, java.math.BigInteger bigInteger, java.math.BigInteger bigInteger2) {
        java.math.BigInteger bigIntegerR;
        org.bouncycastle.math.ec.ECFieldElement eCFieldElementE;
        org.bouncycastle.crypto.params.ECDomainParameters eCDomainParametersG = this.h.g();
        java.math.BigInteger bigIntegerE = eCDomainParametersG.e();
        java.math.BigInteger bigIntegerC = c(bigIntegerE, bArr);
        java.math.BigInteger bigInteger3 = org.bouncycastle.math.ec.ECConstants.b;
        if (bigInteger.compareTo(bigInteger3) < 0 || bigInteger.compareTo(bigIntegerE) >= 0 || bigInteger2.compareTo(bigInteger3) < 0 || bigInteger2.compareTo(bigIntegerE) >= 0) {
            return false;
        }
        java.math.BigInteger bigIntegerO = org.bouncycastle.util.BigIntegers.o(bigIntegerE, bigInteger2);
        org.bouncycastle.math.ec.ECPoint eCPointR = org.bouncycastle.math.ec.ECAlgorithms.r(eCDomainParametersG.b(), bigIntegerC.multiply(bigIntegerO).mod(bigIntegerE), ((org.bouncycastle.crypto.params.ECPublicKeyParameters) this.h).h(), bigInteger.multiply(bigIntegerO).mod(bigIntegerE));
        if (eCPointR.u()) {
            return false;
        }
        org.bouncycastle.math.ec.ECCurve eCCurveI = eCPointR.i();
        if (eCCurveI == null || (bigIntegerR = eCCurveI.r()) == null || bigIntegerR.compareTo(org.bouncycastle.math.ec.ECConstants.f) > 0 || (eCFieldElementE = e(eCCurveI.s(), eCPointR)) == null || eCFieldElementE.k()) {
            return eCPointR.A().f().v().mod(bigIntegerE).equals(bigInteger);
        }
        org.bouncycastle.math.ec.ECFieldElement eCFieldElementQ = eCPointR.q();
        while (eCCurveI.C(bigInteger)) {
            if (eCCurveI.n(bigInteger).l(eCFieldElementE).equals(eCFieldElementQ)) {
                return true;
            }
            bigInteger = bigInteger.add(bigIntegerE);
        }
        return false;
    }

    protected java.math.BigInteger c(java.math.BigInteger bigInteger, byte[] bArr) {
        int iBitLength = bigInteger.bitLength();
        int length = bArr.length * 8;
        java.math.BigInteger bigInteger2 = new java.math.BigInteger(1, bArr);
        return iBitLength < length ? bigInteger2.shiftRight(length - iBitLength) : bigInteger2;
    }

    protected org.bouncycastle.math.ec.ECMultiplier d() {
        return new org.bouncycastle.math.ec.FixedPointCombMultiplier();
    }

    protected org.bouncycastle.math.ec.ECFieldElement e(int i, org.bouncycastle.math.ec.ECPoint eCPoint) {
        if (i != 1) {
            if (i == 2 || i == 3 || i == 4) {
                return eCPoint.s(0).q();
            }
            if (i != 6 && i != 7) {
                return null;
            }
        }
        return eCPoint.s(0);
    }

    protected java.security.SecureRandom f(boolean z, java.security.SecureRandom secureRandom) {
        if (z) {
            return org.bouncycastle.crypto.CryptoServicesRegistrar.e(secureRandom);
        }
        return null;
    }

    @Override // org.bouncycastle.crypto.DSAExt
    public java.math.BigInteger getOrder() {
        return this.h.g().e();
    }

    /* JADX WARN: Code duplicated, block: B:15:0x0035  */
    @Override // org.bouncycastle.crypto.DSA
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.params.ECKeyParameters eCKeyParameters;
        java.security.SecureRandom secureRandomB;
        boolean z2;
        if (z) {
            if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
                org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
                this.h = (org.bouncycastle.crypto.params.ECPrivateKeyParameters) parametersWithRandom.a();
                secureRandomB = parametersWithRandom.b();
            } else {
                eCKeyParameters = (org.bouncycastle.crypto.params.ECPrivateKeyParameters) cipherParameters;
            }
            org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.signers.Utils.c("ECDSA", this.h, z));
            if (z || this.g.c()) {
                z2 = false;
            } else {
                z2 = true;
            }
            this.i = f(z2, secureRandomB);
        }
        eCKeyParameters = (org.bouncycastle.crypto.params.ECPublicKeyParameters) cipherParameters;
        this.h = eCKeyParameters;
        secureRandomB = null;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.signers.Utils.c("ECDSA", this.h, z));
        if (z) {
            z2 = false;
        } else {
            z2 = false;
        }
        this.i = f(z2, secureRandomB);
    }

    public ECDSASigner(org.bouncycastle.crypto.signers.DSAKCalculator dSAKCalculator) {
        this.g = dSAKCalculator;
    }
}
