package org.bouncycastle.crypto.signers;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DSASigner implements org.bouncycastle.crypto.DSAExt {
    private final org.bouncycastle.crypto.signers.DSAKCalculator g;
    private org.bouncycastle.crypto.params.DSAKeyParameters h;
    private java.security.SecureRandom i;

    public DSASigner() {
        this.g = new org.bouncycastle.crypto.signers.RandomDSAKCalculator();
    }

    private java.math.BigInteger c(java.math.BigInteger bigInteger, byte[] bArr) {
        if (bigInteger.bitLength() >= bArr.length * 8) {
            return new java.math.BigInteger(1, bArr);
        }
        int iBitLength = bigInteger.bitLength() / 8;
        byte[] bArr2 = new byte[iBitLength];
        java.lang.System.arraycopy(bArr, 0, bArr2, 0, iBitLength);
        return new java.math.BigInteger(1, bArr2);
    }

    private java.math.BigInteger d(java.math.BigInteger bigInteger, java.security.SecureRandom secureRandom) {
        return org.bouncycastle.util.BigIntegers.f(7, org.bouncycastle.crypto.CryptoServicesRegistrar.e(secureRandom)).add(java.math.BigInteger.valueOf(128L)).multiply(bigInteger);
    }

    @Override // org.bouncycastle.crypto.DSA
    public java.math.BigInteger[] a(byte[] bArr) {
        org.bouncycastle.crypto.params.DSAParameters dSAParametersG = this.h.g();
        java.math.BigInteger bigIntegerC = dSAParametersG.c();
        java.math.BigInteger bigIntegerC2 = c(bigIntegerC, bArr);
        java.math.BigInteger bigIntegerH = ((org.bouncycastle.crypto.params.DSAPrivateKeyParameters) this.h).h();
        if (this.g.c()) {
            this.g.d(bigIntegerC, bigIntegerH, bArr);
        } else {
            this.g.a(bigIntegerC, this.i);
        }
        java.math.BigInteger bigIntegerB = this.g.b();
        java.math.BigInteger bigIntegerMod = dSAParametersG.a().modPow(bigIntegerB.add(d(bigIntegerC, this.i)), dSAParametersG.b()).mod(bigIntegerC);
        return new java.math.BigInteger[]{bigIntegerMod, org.bouncycastle.util.BigIntegers.n(bigIntegerC, bigIntegerB).multiply(bigIntegerC2.add(bigIntegerH.multiply(bigIntegerMod))).mod(bigIntegerC)};
    }

    @Override // org.bouncycastle.crypto.DSA
    public boolean b(byte[] bArr, java.math.BigInteger bigInteger, java.math.BigInteger bigInteger2) {
        org.bouncycastle.crypto.params.DSAParameters dSAParametersG = this.h.g();
        java.math.BigInteger bigIntegerC = dSAParametersG.c();
        java.math.BigInteger bigIntegerC2 = c(bigIntegerC, bArr);
        java.math.BigInteger bigIntegerValueOf = java.math.BigInteger.valueOf(0L);
        if (bigIntegerValueOf.compareTo(bigInteger) >= 0 || bigIntegerC.compareTo(bigInteger) <= 0 || bigIntegerValueOf.compareTo(bigInteger2) >= 0 || bigIntegerC.compareTo(bigInteger2) <= 0) {
            return false;
        }
        java.math.BigInteger bigIntegerO = org.bouncycastle.util.BigIntegers.o(bigIntegerC, bigInteger2);
        java.math.BigInteger bigIntegerMod = bigIntegerC2.multiply(bigIntegerO).mod(bigIntegerC);
        java.math.BigInteger bigIntegerMod2 = bigInteger.multiply(bigIntegerO).mod(bigIntegerC);
        java.math.BigInteger bigIntegerB = dSAParametersG.b();
        return dSAParametersG.a().modPow(bigIntegerMod, bigIntegerB).multiply(((org.bouncycastle.crypto.params.DSAPublicKeyParameters) this.h).h().modPow(bigIntegerMod2, bigIntegerB)).mod(bigIntegerB).mod(bigIntegerC).equals(bigInteger);
    }

    protected java.security.SecureRandom e(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().c();
    }

    /* 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.DSAKeyParameters dSAKeyParameters;
        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.DSAPrivateKeyParameters) parametersWithRandom.a();
                secureRandomB = parametersWithRandom.b();
            } else {
                dSAKeyParameters = (org.bouncycastle.crypto.params.DSAPrivateKeyParameters) cipherParameters;
            }
            org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.signers.Utils.b("DSA", this.h, z));
            if (z || this.g.c()) {
                z2 = false;
            } else {
                z2 = true;
            }
            this.i = e(z2, secureRandomB);
        }
        dSAKeyParameters = (org.bouncycastle.crypto.params.DSAPublicKeyParameters) cipherParameters;
        this.h = dSAKeyParameters;
        secureRandomB = null;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.signers.Utils.b("DSA", this.h, z));
        if (z) {
            z2 = false;
        } else {
            z2 = false;
        }
        this.i = e(z2, secureRandomB);
    }

    public DSASigner(org.bouncycastle.crypto.signers.DSAKCalculator dSAKCalculator) {
        this.g = dSAKCalculator;
    }
}
