package org.bouncycastle.crypto.signers;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class SM2Signer implements org.bouncycastle.crypto.Signer, org.bouncycastle.math.ec.ECConstants {
    private final org.bouncycastle.crypto.signers.DSAKCalculator g;
    private final org.bouncycastle.crypto.Digest h;
    private final org.bouncycastle.crypto.signers.DSAEncoding i;
    private int j;
    private org.bouncycastle.crypto.params.ECDomainParameters k;
    private org.bouncycastle.math.ec.ECPoint l;
    private org.bouncycastle.crypto.params.ECKeyParameters m;
    private byte[] n;

    private static final class State {
    }

    public SM2Signer() {
        this(org.bouncycastle.crypto.signers.StandardDSAEncoding.a, new org.bouncycastle.crypto.digests.SM3Digest());
    }

    private void c(org.bouncycastle.crypto.Digest digest, org.bouncycastle.math.ec.ECFieldElement eCFieldElement) {
        byte[] bArrF = eCFieldElement.f();
        digest.update(bArrF, 0, bArrF.length);
    }

    private void d(org.bouncycastle.crypto.Digest digest, byte[] bArr) {
        int length = bArr.length * 8;
        digest.update((byte) (length >>> 8));
        digest.update((byte) length);
        digest.update(bArr, 0, bArr.length);
    }

    private void f() {
        int i = this.j;
        if (i != 1) {
            if (i != 2) {
                throw new java.lang.IllegalStateException("SM2Signer needs to be initialized");
            }
        } else {
            org.bouncycastle.crypto.Digest digest = this.h;
            byte[] bArr = this.n;
            digest.update(bArr, 0, bArr.length);
            this.j = 2;
        }
    }

    private byte[] h() {
        byte[] bArr = new byte[this.h.getDigestSize()];
        this.h.doFinal(bArr, 0);
        return bArr;
    }

    private byte[] i(byte[] bArr) {
        d(this.h, bArr);
        c(this.h, this.k.a().o());
        c(this.h, this.k.a().q());
        c(this.h, this.k.b().f());
        c(this.h, this.k.b().g());
        c(this.h, this.l.f());
        c(this.h, this.l.g());
        return h();
    }

    private boolean k(java.math.BigInteger bigInteger, java.math.BigInteger bigInteger2) {
        java.math.BigInteger bigIntegerE = this.k.e();
        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 bigIntegerE2 = e(bigIntegerE, h());
        java.math.BigInteger bigIntegerMod = bigInteger.add(bigInteger2).mod(bigIntegerE);
        if (bigIntegerMod.equals(org.bouncycastle.math.ec.ECConstants.a)) {
            return false;
        }
        org.bouncycastle.math.ec.ECPoint eCPointA = org.bouncycastle.math.ec.ECAlgorithms.r(this.k.b(), bigInteger2, ((org.bouncycastle.crypto.params.ECPublicKeyParameters) this.m).h(), bigIntegerMod).A();
        if (eCPointA.u()) {
            return false;
        }
        return bigIntegerE2.add(eCPointA.f().v()).mod(bigIntegerE).equals(bigInteger);
    }

    @Override // org.bouncycastle.crypto.Signer
    public boolean a(byte[] bArr) {
        f();
        try {
            java.math.BigInteger[] bigIntegerArrA = this.i.a(this.k.e(), bArr);
            return k(bigIntegerArrA[0], bigIntegerArrA[1]);
        } catch (java.lang.Exception unused) {
            return false;
        } finally {
            j();
        }
    }

    @Override // org.bouncycastle.crypto.Signer
    public byte[] b() {
        f();
        byte[] bArrH = h();
        java.math.BigInteger bigIntegerE = this.k.e();
        java.math.BigInteger bigIntegerE2 = e(bigIntegerE, bArrH);
        java.math.BigInteger bigIntegerH = ((org.bouncycastle.crypto.params.ECPrivateKeyParameters) this.m).h();
        org.bouncycastle.math.ec.ECMultiplier eCMultiplierG = g();
        while (true) {
            java.math.BigInteger bigIntegerB = this.g.b();
            java.math.BigInteger bigIntegerMod = bigIntegerE2.add(eCMultiplierG.a(this.k.b(), bigIntegerB).A().f().v()).mod(bigIntegerE);
            java.math.BigInteger bigInteger = org.bouncycastle.math.ec.ECConstants.a;
            if (!bigIntegerMod.equals(bigInteger) && !bigIntegerMod.add(bigIntegerB).equals(bigIntegerE)) {
                java.math.BigInteger bigIntegerMod2 = org.bouncycastle.util.BigIntegers.n(bigIntegerE, bigIntegerH.add(org.bouncycastle.math.ec.ECConstants.b)).multiply(bigIntegerB.subtract(bigIntegerMod.multiply(bigIntegerH)).mod(bigIntegerE)).mod(bigIntegerE);
                if (!bigIntegerMod2.equals(bigInteger)) {
                    try {
                        try {
                            byte[] bArrB = this.i.b(this.k.e(), bigIntegerMod, bigIntegerMod2);
                            j();
                            return bArrB;
                        } catch (java.lang.Exception e) {
                            throw new org.bouncycastle.crypto.CryptoException("unable to encode signature: " + e.getMessage(), e);
                        }
                    } catch (java.lang.Throwable th) {
                        j();
                        throw th;
                    }
                }
            }
        }
    }

    protected java.math.BigInteger e(java.math.BigInteger bigInteger, byte[] bArr) {
        return new java.math.BigInteger(1, bArr);
    }

    protected org.bouncycastle.math.ec.ECMultiplier g() {
        return new org.bouncycastle.math.ec.FixedPointCombMultiplier();
    }

    @Override // org.bouncycastle.crypto.Signer
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        byte[] bArrB;
        org.bouncycastle.math.ec.ECPoint eCPointH;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithID) {
            org.bouncycastle.crypto.params.ParametersWithID parametersWithID = (org.bouncycastle.crypto.params.ParametersWithID) cipherParameters;
            org.bouncycastle.crypto.CipherParameters cipherParametersB = parametersWithID.b();
            byte[] bArrA = parametersWithID.a();
            if (bArrA.length >= 8192) {
                throw new java.lang.IllegalArgumentException("SM2 user ID must be less than 2^13 bits long");
            }
            bArrB = bArrA;
            cipherParameters = cipherParametersB;
        } else {
            bArrB = org.bouncycastle.util.encoders.Hex.b("31323334353637383132333435363738");
        }
        if (z) {
            if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
                org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
                org.bouncycastle.crypto.params.ECKeyParameters eCKeyParameters = (org.bouncycastle.crypto.params.ECKeyParameters) parametersWithRandom.a();
                this.m = eCKeyParameters;
                org.bouncycastle.crypto.params.ECDomainParameters eCDomainParametersG = eCKeyParameters.g();
                this.k = eCDomainParametersG;
                this.g.a(eCDomainParametersG.e(), parametersWithRandom.b());
            } else {
                org.bouncycastle.crypto.params.ECKeyParameters eCKeyParameters2 = (org.bouncycastle.crypto.params.ECKeyParameters) cipherParameters;
                this.m = eCKeyParameters2;
                org.bouncycastle.crypto.params.ECDomainParameters eCDomainParametersG2 = eCKeyParameters2.g();
                this.k = eCDomainParametersG2;
                this.g.a(eCDomainParametersG2.e(), org.bouncycastle.crypto.CryptoServicesRegistrar.d());
            }
            java.math.BigInteger bigIntegerH = ((org.bouncycastle.crypto.params.ECPrivateKeyParameters) this.m).h();
            java.math.BigInteger bigIntegerSubtract = this.k.e().subtract(org.bouncycastle.util.BigIntegers.b);
            if (bigIntegerH.compareTo(org.bouncycastle.math.ec.ECConstants.b) < 0 || bigIntegerH.compareTo(bigIntegerSubtract) >= 0) {
                throw new java.lang.IllegalArgumentException("SM2 private key out of range");
            }
            eCPointH = g().a(this.k.b(), bigIntegerH).A();
        } else {
            org.bouncycastle.crypto.params.ECKeyParameters eCKeyParameters3 = (org.bouncycastle.crypto.params.ECKeyParameters) cipherParameters;
            this.m = eCKeyParameters3;
            this.k = eCKeyParameters3.g();
            eCPointH = ((org.bouncycastle.crypto.params.ECPublicKeyParameters) this.m).h();
        }
        this.l = eCPointH;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.signers.Utils.c("ECNR", this.m, z));
        this.h.reset();
        this.n = i(bArrB);
        this.j = 1;
    }

    public void j() {
        int i = this.j;
        if (i != 1) {
            if (i != 2) {
                throw new java.lang.IllegalStateException("SM2Signer needs to be initialized");
            }
            this.h.reset();
            this.j = 1;
        }
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte b) {
        f();
        this.h.update(b);
    }

    public SM2Signer(org.bouncycastle.crypto.Digest digest) {
        this(org.bouncycastle.crypto.signers.StandardDSAEncoding.a, digest);
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte[] bArr, int i, int i2) {
        f();
        this.h.update(bArr, i, i2);
    }

    public SM2Signer(org.bouncycastle.crypto.signers.DSAEncoding dSAEncoding, org.bouncycastle.crypto.Digest digest) {
        this.g = new org.bouncycastle.crypto.signers.RandomDSAKCalculator();
        this.j = 0;
        this.i = dSAEncoding;
        this.h = digest;
    }
}
