package org.bouncycastle.crypto.signers;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ECNRSigner implements org.bouncycastle.crypto.DSAExt {
    private boolean g;
    private org.bouncycastle.crypto.params.ECKeyParameters h;
    private java.security.SecureRandom i;

    private java.math.BigInteger c(org.bouncycastle.crypto.params.ECPublicKeyParameters eCPublicKeyParameters, java.math.BigInteger bigInteger, java.math.BigInteger bigInteger2) {
        java.math.BigInteger bigIntegerE = eCPublicKeyParameters.g().e();
        if (bigInteger.compareTo(org.bouncycastle.math.ec.ECConstants.b) < 0 || bigInteger.compareTo(bigIntegerE) >= 0 || bigInteger2.compareTo(org.bouncycastle.math.ec.ECConstants.a) < 0 || bigInteger2.compareTo(bigIntegerE) >= 0) {
            return null;
        }
        org.bouncycastle.math.ec.ECPoint eCPointA = org.bouncycastle.math.ec.ECAlgorithms.r(eCPublicKeyParameters.g().b(), bigInteger2, eCPublicKeyParameters.h(), bigInteger).A();
        if (eCPointA.u()) {
            return null;
        }
        return bigInteger.subtract(eCPointA.f().v()).mod(bigIntegerE);
    }

    @Override // org.bouncycastle.crypto.DSA
    public java.math.BigInteger[] a(byte[] bArr) {
        org.bouncycastle.crypto.AsymmetricCipherKeyPair asymmetricCipherKeyPairA;
        java.math.BigInteger bigIntegerMod;
        if (!this.g) {
            throw new java.lang.IllegalStateException("not initialised for signing");
        }
        java.math.BigInteger order = getOrder();
        java.math.BigInteger bigInteger = new java.math.BigInteger(1, bArr);
        org.bouncycastle.crypto.params.ECPrivateKeyParameters eCPrivateKeyParameters = (org.bouncycastle.crypto.params.ECPrivateKeyParameters) this.h;
        if (bigInteger.compareTo(order) >= 0) {
            throw new org.bouncycastle.crypto.DataLengthException("input too large for ECNR key");
        }
        do {
            org.bouncycastle.crypto.generators.ECKeyPairGenerator eCKeyPairGenerator = new org.bouncycastle.crypto.generators.ECKeyPairGenerator();
            eCKeyPairGenerator.b(new org.bouncycastle.crypto.params.ECKeyGenerationParameters(eCPrivateKeyParameters.g(), this.i));
            asymmetricCipherKeyPairA = eCKeyPairGenerator.a();
            bigIntegerMod = ((org.bouncycastle.crypto.params.ECPublicKeyParameters) asymmetricCipherKeyPairA.b()).h().f().v().add(bigInteger).mod(order);
        } while (bigIntegerMod.equals(org.bouncycastle.math.ec.ECConstants.a));
        return new java.math.BigInteger[]{bigIntegerMod, ((org.bouncycastle.crypto.params.ECPrivateKeyParameters) asymmetricCipherKeyPairA.a()).h().subtract(bigIntegerMod.multiply(eCPrivateKeyParameters.h())).mod(order)};
    }

    @Override // org.bouncycastle.crypto.DSA
    public boolean b(byte[] bArr, java.math.BigInteger bigInteger, java.math.BigInteger bigInteger2) {
        if (this.g) {
            throw new java.lang.IllegalStateException("not initialised for verifying");
        }
        org.bouncycastle.crypto.params.ECPublicKeyParameters eCPublicKeyParameters = (org.bouncycastle.crypto.params.ECPublicKeyParameters) this.h;
        java.math.BigInteger bigIntegerE = eCPublicKeyParameters.g().e();
        int iBitLength = bigIntegerE.bitLength();
        java.math.BigInteger bigInteger3 = new java.math.BigInteger(1, bArr);
        if (bigInteger3.bitLength() > iBitLength) {
            throw new org.bouncycastle.crypto.DataLengthException("input too large for ECNR key.");
        }
        java.math.BigInteger bigIntegerC = c(eCPublicKeyParameters, bigInteger, bigInteger2);
        return bigIntegerC != null && bigIntegerC.equals(bigInteger3.mod(bigIntegerE));
    }

    @Override // org.bouncycastle.crypto.DSAExt
    public java.math.BigInteger getOrder() {
        return this.h.g().e();
    }

    @Override // org.bouncycastle.crypto.DSA
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.params.ECKeyParameters eCKeyParameters;
        this.g = z;
        if (z) {
            if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
                org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
                this.i = parametersWithRandom.b();
                cipherParameters = parametersWithRandom.a();
            } else {
                this.i = org.bouncycastle.crypto.CryptoServicesRegistrar.d();
            }
            eCKeyParameters = (org.bouncycastle.crypto.params.ECPrivateKeyParameters) cipherParameters;
        } else {
            eCKeyParameters = (org.bouncycastle.crypto.params.ECPublicKeyParameters) cipherParameters;
        }
        this.h = eCKeyParameters;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.signers.Utils.c("ECNR", this.h, z));
    }
}
