package org.bouncycastle.crypto.signers;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DSTU4145Signer implements org.bouncycastle.crypto.DSAExt {
    private static final java.math.BigInteger i = java.math.BigInteger.valueOf(1);
    private org.bouncycastle.crypto.params.ECKeyParameters g;
    private java.security.SecureRandom h;

    private static java.math.BigInteger d(java.math.BigInteger bigInteger, org.bouncycastle.math.ec.ECFieldElement eCFieldElement) {
        return g(eCFieldElement.v(), bigInteger.bitLength() - 1);
    }

    private static java.math.BigInteger e(java.math.BigInteger bigInteger, java.security.SecureRandom secureRandom) {
        return org.bouncycastle.util.BigIntegers.f(bigInteger.bitLength() - 1, secureRandom);
    }

    private static org.bouncycastle.math.ec.ECFieldElement f(org.bouncycastle.math.ec.ECCurve eCCurve, byte[] bArr) {
        return eCCurve.n(g(new java.math.BigInteger(1, org.bouncycastle.util.Arrays.f0(bArr)), eCCurve.w()));
    }

    private static java.math.BigInteger g(java.math.BigInteger bigInteger, int i2) {
        return bigInteger.bitLength() > i2 ? bigInteger.mod(i.shiftLeft(i2)) : bigInteger;
    }

    @Override // org.bouncycastle.crypto.DSA
    public java.math.BigInteger[] a(byte[] bArr) {
        org.bouncycastle.crypto.params.ECDomainParameters eCDomainParametersG = this.g.g();
        org.bouncycastle.math.ec.ECCurve eCCurveA = eCDomainParametersG.a();
        org.bouncycastle.math.ec.ECFieldElement eCFieldElementF = f(eCCurveA, bArr);
        if (eCFieldElementF.k()) {
            eCFieldElementF = eCCurveA.n(i);
        }
        java.math.BigInteger bigIntegerE = eCDomainParametersG.e();
        java.math.BigInteger bigIntegerH = ((org.bouncycastle.crypto.params.ECPrivateKeyParameters) this.g).h();
        org.bouncycastle.math.ec.ECMultiplier eCMultiplierC = c();
        while (true) {
            java.math.BigInteger bigIntegerE2 = e(bigIntegerE, this.h);
            org.bouncycastle.math.ec.ECFieldElement eCFieldElementF2 = eCMultiplierC.a(eCDomainParametersG.b(), bigIntegerE2).A().f();
            if (!eCFieldElementF2.k()) {
                java.math.BigInteger bigIntegerD = d(bigIntegerE, eCFieldElementF.l(eCFieldElementF2));
                if (bigIntegerD.signum() != 0) {
                    java.math.BigInteger bigIntegerMod = bigIntegerD.multiply(bigIntegerH).add(bigIntegerE2).mod(bigIntegerE);
                    if (bigIntegerMod.signum() != 0) {
                        return new java.math.BigInteger[]{bigIntegerD, bigIntegerMod};
                    }
                } else {
                    continue;
                }
            }
        }
    }

    @Override // org.bouncycastle.crypto.DSA
    public boolean b(byte[] bArr, java.math.BigInteger bigInteger, java.math.BigInteger bigInteger2) {
        if (bigInteger.signum() > 0 && bigInteger2.signum() > 0) {
            org.bouncycastle.crypto.params.ECDomainParameters eCDomainParametersG = this.g.g();
            java.math.BigInteger bigIntegerE = eCDomainParametersG.e();
            if (bigInteger.compareTo(bigIntegerE) < 0 && bigInteger2.compareTo(bigIntegerE) < 0) {
                org.bouncycastle.math.ec.ECCurve eCCurveA = eCDomainParametersG.a();
                org.bouncycastle.math.ec.ECFieldElement eCFieldElementF = f(eCCurveA, bArr);
                if (eCFieldElementF.k()) {
                    eCFieldElementF = eCCurveA.n(i);
                }
                org.bouncycastle.math.ec.ECPoint eCPointA = org.bouncycastle.math.ec.ECAlgorithms.r(eCDomainParametersG.b(), bigInteger2, ((org.bouncycastle.crypto.params.ECPublicKeyParameters) this.g).h(), bigInteger).A();
                if (!eCPointA.u() && d(bigIntegerE, eCFieldElementF.l(eCPointA.f())).compareTo(bigInteger) == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    protected org.bouncycastle.math.ec.ECMultiplier c() {
        return new org.bouncycastle.math.ec.FixedPointCombMultiplier();
    }

    @Override // org.bouncycastle.crypto.DSAExt
    public java.math.BigInteger getOrder() {
        return this.g.g().e();
    }

    @Override // org.bouncycastle.crypto.DSA
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.params.ECKeyParameters eCKeyParameters;
        if (z) {
            if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
                org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
                this.h = parametersWithRandom.b();
                cipherParameters = parametersWithRandom.a();
            } else {
                this.h = org.bouncycastle.crypto.CryptoServicesRegistrar.d();
            }
            eCKeyParameters = (org.bouncycastle.crypto.params.ECPrivateKeyParameters) cipherParameters;
        } else {
            eCKeyParameters = (org.bouncycastle.crypto.params.ECPublicKeyParameters) cipherParameters;
        }
        this.g = eCKeyParameters;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.signers.Utils.c("DSTU4145", this.g, z));
    }
}
