package org.bouncycastle.crypto.signers;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ISO9796d2Signer implements org.bouncycastle.crypto.SignerWithRecovery {
    private org.bouncycastle.crypto.Digest g;
    private org.bouncycastle.crypto.AsymmetricBlockCipher h;
    private int i;
    private int j;
    private byte[] k;
    private byte[] l;
    private int m;
    private boolean n;
    private byte[] o;
    private byte[] p;
    private byte[] q;

    public ISO9796d2Signer(org.bouncycastle.crypto.AsymmetricBlockCipher asymmetricBlockCipher, org.bouncycastle.crypto.Digest digest, boolean z) {
        int iIntValue;
        this.h = asymmetricBlockCipher;
        this.g = digest;
        if (z) {
            iIntValue = 188;
        } else {
            java.lang.Integer numA = org.bouncycastle.crypto.signers.ISOTrailers.a(digest);
            if (numA == null) {
                throw new java.lang.IllegalArgumentException("no valid trailer for digest: " + digest.getAlgorithmName());
            }
            iIntValue = numA.intValue();
        }
        this.i = iIntValue;
    }

    private void c(byte[] bArr) {
        for (int i = 0; i != bArr.length; i++) {
            bArr[i] = 0;
        }
    }

    private boolean d(byte[] bArr, byte[] bArr2) {
        boolean z;
        int i = this.m;
        byte[] bArr3 = this.l;
        if (i > bArr3.length) {
            z = bArr3.length <= bArr2.length;
            for (int i2 = 0; i2 != this.l.length; i2++) {
                if (bArr[i2] != bArr2[i2]) {
                    z = false;
                }
            }
            return z;
        }
        z = i == bArr2.length;
        for (int i3 = 0; i3 != bArr2.length; i3++) {
            if (bArr[i3] != bArr2[i3]) {
                z = false;
            }
        }
        return z;
    }

    private boolean f(byte[] bArr) {
        this.m = 0;
        c(this.l);
        c(bArr);
        return false;
    }

    @Override // org.bouncycastle.crypto.Signer
    public boolean a(byte[] bArr) {
        byte[] bArrProcessBlock;
        int i;
        byte[] bArr2 = this.p;
        if (bArr2 == null) {
            try {
                bArrProcessBlock = this.h.processBlock(bArr, 0, bArr.length);
            } catch (java.lang.Exception unused) {
                return false;
            }
        } else {
            if (!org.bouncycastle.util.Arrays.d(bArr2, bArr)) {
                throw new java.lang.IllegalStateException("updateWithRecoveredMessage called on different signature");
            }
            bArrProcessBlock = this.q;
            this.p = null;
            this.q = null;
        }
        if (((bArrProcessBlock[0] & 192) ^ 64) == 0 && ((bArrProcessBlock[bArrProcessBlock.length - 1] & 15) ^ 12) == 0) {
            if (((bArrProcessBlock[bArrProcessBlock.length - 1] & 255) ^ 188) == 0) {
                i = 1;
            } else {
                i = 2;
                int i2 = ((bArrProcessBlock[bArrProcessBlock.length - 2] & 255) << 8) | (bArrProcessBlock[bArrProcessBlock.length - 1] & 255);
                java.lang.Integer numA = org.bouncycastle.crypto.signers.ISOTrailers.a(this.g);
                if (numA == null) {
                    throw new java.lang.IllegalArgumentException("unrecognised hash in signature");
                }
                int iIntValue = numA.intValue();
                if (i2 != iIntValue && (iIntValue != 15052 || i2 != 16588)) {
                    throw new java.lang.IllegalStateException("signer initialised with wrong digest for trailer " + i2);
                }
            }
            int i3 = 0;
            while (i3 != bArrProcessBlock.length && ((bArrProcessBlock[i3] & 15) ^ 10) != 0) {
                i3++;
            }
            int i4 = i3 + 1;
            int digestSize = this.g.getDigestSize();
            byte[] bArr3 = new byte[digestSize];
            int length = (bArrProcessBlock.length - i) - digestSize;
            int i5 = length - i4;
            if (i5 <= 0) {
                return f(bArrProcessBlock);
            }
            if ((bArrProcessBlock[0] & 32) == 0) {
                this.n = true;
                if (this.m > i5) {
                    return f(bArrProcessBlock);
                }
                this.g.reset();
                this.g.update(bArrProcessBlock, i4, i5);
                this.g.doFinal(bArr3, 0);
                boolean z = true;
                for (int i6 = 0; i6 != digestSize; i6++) {
                    int i7 = length + i6;
                    byte b = (byte) (bArrProcessBlock[i7] ^ bArr3[i6]);
                    bArrProcessBlock[i7] = b;
                    if (b != 0) {
                        z = false;
                    }
                }
                if (!z) {
                    return f(bArrProcessBlock);
                }
                byte[] bArr4 = new byte[i5];
                this.o = bArr4;
                java.lang.System.arraycopy(bArrProcessBlock, i4, bArr4, 0, bArr4.length);
            } else {
                this.n = false;
                this.g.doFinal(bArr3, 0);
                boolean z2 = true;
                for (int i8 = 0; i8 != digestSize; i8++) {
                    int i9 = length + i8;
                    byte b2 = (byte) (bArrProcessBlock[i9] ^ bArr3[i8]);
                    bArrProcessBlock[i9] = b2;
                    if (b2 != 0) {
                        z2 = false;
                    }
                }
                if (!z2) {
                    return f(bArrProcessBlock);
                }
                byte[] bArr5 = new byte[i5];
                this.o = bArr5;
                java.lang.System.arraycopy(bArrProcessBlock, i4, bArr5, 0, bArr5.length);
            }
            if (this.m != 0 && !d(this.l, this.o)) {
                return f(bArrProcessBlock);
            }
            c(this.l);
            c(bArrProcessBlock);
            this.m = 0;
            return true;
        }
        return f(bArrProcessBlock);
    }

    @Override // org.bouncycastle.crypto.Signer
    public byte[] b() {
        int length;
        int i;
        int i2;
        int i3;
        int digestSize = this.g.getDigestSize();
        if (this.i == 188) {
            byte[] bArr = this.k;
            length = (bArr.length - digestSize) - 1;
            this.g.doFinal(bArr, length);
            byte[] bArr2 = this.k;
            bArr2[bArr2.length - 1] = -68;
            i = 8;
        } else {
            byte[] bArr3 = this.k;
            length = (bArr3.length - digestSize) - 2;
            this.g.doFinal(bArr3, length);
            byte[] bArr4 = this.k;
            int length2 = bArr4.length - 2;
            int i4 = this.i;
            bArr4[length2] = (byte) (i4 >>> 8);
            bArr4[bArr4.length - 1] = (byte) i4;
            i = 16;
        }
        int i5 = this.m;
        int i6 = ((((digestSize + i5) * 8) + i) + 4) - this.j;
        if (i6 > 0) {
            int i7 = i5 - ((i6 + 7) / 8);
            i2 = length - i7;
            java.lang.System.arraycopy(this.l, 0, this.k, i2, i7);
            this.o = new byte[i7];
            i3 = 96;
        } else {
            i2 = length - i5;
            java.lang.System.arraycopy(this.l, 0, this.k, i2, i5);
            this.o = new byte[this.m];
            i3 = 64;
        }
        int i8 = i2 - 1;
        if (i8 > 0) {
            for (int i9 = i8; i9 != 0; i9--) {
                this.k[i9] = -69;
            }
            byte[] bArr5 = this.k;
            bArr5[i8] = (byte) (bArr5[i8] ^ 1);
            bArr5[0] = 11;
            bArr5[0] = (byte) (11 | i3);
        } else {
            byte[] bArr6 = this.k;
            bArr6[0] = 10;
            bArr6[0] = (byte) (10 | i3);
        }
        org.bouncycastle.crypto.AsymmetricBlockCipher asymmetricBlockCipher = this.h;
        byte[] bArr7 = this.k;
        byte[] bArrProcessBlock = asymmetricBlockCipher.processBlock(bArr7, 0, bArr7.length);
        this.n = (i3 & 32) == 0;
        byte[] bArr8 = this.l;
        byte[] bArr9 = this.o;
        java.lang.System.arraycopy(bArr8, 0, bArr9, 0, bArr9.length);
        this.m = 0;
        c(this.l);
        c(this.k);
        return bArrProcessBlock;
    }

    public void e() {
        this.g.reset();
        this.m = 0;
        c(this.l);
        byte[] bArr = this.o;
        if (bArr != null) {
            c(bArr);
        }
        this.o = null;
        this.n = false;
        if (this.p != null) {
            this.p = null;
            c(this.q);
            this.q = null;
        }
    }

    @Override // org.bouncycastle.crypto.Signer
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.params.RSAKeyParameters rSAKeyParameters = (org.bouncycastle.crypto.params.RSAKeyParameters) cipherParameters;
        this.h.init(z, rSAKeyParameters);
        int iBitLength = rSAKeyParameters.i().bitLength();
        this.j = iBitLength;
        byte[] bArr = new byte[(iBitLength + 7) / 8];
        this.k = bArr;
        int i = this.i;
        int length = bArr.length;
        if (i == 188) {
            this.l = new byte[(length - this.g.getDigestSize()) - 2];
        } else {
            this.l = new byte[(length - this.g.getDigestSize()) - 3];
        }
        e();
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte b) {
        this.g.update(b);
        int i = this.m;
        byte[] bArr = this.l;
        if (i < bArr.length) {
            bArr[i] = b;
        }
        this.m = i + 1;
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte[] bArr, int i, int i2) {
        while (i2 > 0 && this.m < this.l.length) {
            update(bArr[i]);
            i++;
            i2--;
        }
        this.g.update(bArr, i, i2);
        this.m += i2;
    }
}
