package org.bouncycastle.crypto.signers;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ISO9796d2PSSSigner implements org.bouncycastle.crypto.SignerWithRecovery {
    private org.bouncycastle.crypto.Digest g;
    private org.bouncycastle.crypto.AsymmetricBlockCipher h;
    private java.security.SecureRandom i;
    private byte[] j;
    private int k;
    private int l;
    private int m;
    private byte[] n;
    private byte[] o;
    private int p;
    private int q;
    private boolean r;
    private byte[] s;
    private byte[] t;
    private byte[] u;
    private int v;
    private int w;

    private void c(int i, byte[] bArr) {
        bArr[0] = (byte) (i >>> 24);
        bArr[1] = (byte) (i >>> 16);
        bArr[2] = (byte) (i >>> 8);
        bArr[3] = (byte) i;
    }

    private void d(long j, byte[] bArr) {
        bArr[0] = (byte) (j >>> 56);
        bArr[1] = (byte) (j >>> 48);
        bArr[2] = (byte) (j >>> 40);
        bArr[3] = (byte) (j >>> 32);
        bArr[4] = (byte) (j >>> 24);
        bArr[5] = (byte) (j >>> 16);
        bArr[6] = (byte) (j >>> 8);
        bArr[7] = (byte) j;
    }

    private void e(byte[] bArr) {
        for (int i = 0; i != bArr.length; i++) {
            bArr[i] = 0;
        }
    }

    private boolean f(byte[] bArr, byte[] bArr2) {
        boolean z = this.p == bArr2.length;
        for (int i = 0; i != bArr2.length; i++) {
            if (bArr[i] != bArr2[i]) {
                z = false;
            }
        }
        return z;
    }

    private byte[] g(byte[] bArr, int i, int i2, int i3) {
        int i4;
        byte[] bArr2 = new byte[i3];
        byte[] bArr3 = new byte[this.k];
        byte[] bArr4 = new byte[4];
        this.g.reset();
        int i5 = 0;
        while (true) {
            i4 = this.k;
            if (i5 >= i3 / i4) {
                break;
            }
            c(i5, bArr4);
            this.g.update(bArr, i, i2);
            this.g.update(bArr4, 0, 4);
            this.g.doFinal(bArr3, 0);
            int i6 = this.k;
            java.lang.System.arraycopy(bArr3, 0, bArr2, i5 * i6, i6);
            i5++;
        }
        if (i4 * i5 < i3) {
            c(i5, bArr4);
            this.g.update(bArr, i, i2);
            this.g.update(bArr4, 0, 4);
            this.g.doFinal(bArr3, 0);
            int i7 = this.k;
            java.lang.System.arraycopy(bArr3, 0, bArr2, i5 * i7, i3 - (i5 * i7));
        }
        return bArr2;
    }

    @Override // org.bouncycastle.crypto.Signer
    public boolean a(byte[] bArr) {
        int i = this.k;
        byte[] bArr2 = new byte[i];
        this.g.doFinal(bArr2, 0);
        byte[] bArr3 = this.t;
        if (bArr3 == null) {
            try {
                i(bArr);
            } catch (java.lang.Exception unused) {
                return false;
            }
        } else if (!org.bouncycastle.util.Arrays.d(bArr3, bArr)) {
            throw new java.lang.IllegalStateException("updateWithRecoveredMessage called on different signature");
        }
        byte[] bArr4 = this.u;
        int i2 = this.v;
        int i3 = this.w;
        this.t = null;
        this.u = null;
        byte[] bArr5 = new byte[8];
        d(this.s.length * 8, bArr5);
        this.g.update(bArr5, 0, 8);
        byte[] bArr6 = this.s;
        if (bArr6.length != 0) {
            this.g.update(bArr6, 0, bArr6.length);
        }
        this.g.update(bArr2, 0, i);
        byte[] bArr7 = this.j;
        if (bArr7 != null) {
            this.g.update(bArr7, 0, bArr7.length);
        } else {
            this.g.update(bArr4, i2 + this.s.length, this.q);
        }
        int digestSize = this.g.getDigestSize();
        byte[] bArr8 = new byte[digestSize];
        this.g.doFinal(bArr8, 0);
        int length = (bArr4.length - i3) - digestSize;
        boolean z = true;
        for (int i4 = 0; i4 != digestSize; i4++) {
            if (bArr8[i4] != bArr4[length + i4]) {
                z = false;
            }
        }
        e(bArr4);
        e(bArr8);
        if (!z) {
            this.r = false;
            this.p = 0;
            e(this.s);
            return false;
        }
        if (this.p == 0 || f(this.o, this.s)) {
            this.p = 0;
            e(this.o);
            return true;
        }
        this.p = 0;
        e(this.o);
        return false;
    }

    @Override // org.bouncycastle.crypto.Signer
    public byte[] b() {
        int digestSize = this.g.getDigestSize();
        byte[] bArr = new byte[digestSize];
        this.g.doFinal(bArr, 0);
        byte[] bArr2 = new byte[8];
        d(this.p * 8, bArr2);
        this.g.update(bArr2, 0, 8);
        this.g.update(this.o, 0, this.p);
        this.g.update(bArr, 0, digestSize);
        byte[] bArr3 = this.j;
        if (bArr3 == null) {
            bArr3 = new byte[this.q];
            this.i.nextBytes(bArr3);
        }
        this.g.update(bArr3, 0, bArr3.length);
        int digestSize2 = this.g.getDigestSize();
        byte[] bArr4 = new byte[digestSize2];
        this.g.doFinal(bArr4, 0);
        int i = this.l == 188 ? 1 : 2;
        byte[] bArr5 = this.n;
        int length = bArr5.length;
        int i2 = this.p;
        int length2 = (((length - i2) - bArr3.length) - this.k) - i;
        bArr5[length2 - 1] = 1;
        java.lang.System.arraycopy(this.o, 0, bArr5, length2, i2);
        java.lang.System.arraycopy(bArr3, 0, this.n, length2 + this.p, bArr3.length);
        byte[] bArrG = g(bArr4, 0, digestSize2, (this.n.length - this.k) - i);
        for (int i3 = 0; i3 != bArrG.length; i3++) {
            byte[] bArr6 = this.n;
            bArr6[i3] = (byte) (bArr6[i3] ^ bArrG[i3]);
        }
        byte[] bArr7 = this.n;
        int length3 = bArr7.length;
        int i4 = this.k;
        java.lang.System.arraycopy(bArr4, 0, bArr7, (length3 - i4) - i, i4);
        int i5 = this.l;
        if (i5 == 188) {
            byte[] bArr8 = this.n;
            bArr8[bArr8.length - 1] = -68;
        } else {
            byte[] bArr9 = this.n;
            bArr9[bArr9.length - 2] = (byte) (i5 >>> 8);
            bArr9[bArr9.length - 1] = (byte) i5;
        }
        byte[] bArr10 = this.n;
        bArr10[0] = (byte) (bArr10[0] & 127);
        byte[] bArrProcessBlock = this.h.processBlock(bArr10, 0, bArr10.length);
        int i6 = this.p;
        byte[] bArr11 = new byte[i6];
        this.s = bArr11;
        byte[] bArr12 = this.o;
        this.r = i6 <= bArr12.length;
        java.lang.System.arraycopy(bArr12, 0, bArr11, 0, bArr11.length);
        e(this.o);
        e(this.n);
        this.p = 0;
        return bArrProcessBlock;
    }

    public void h() {
        this.g.reset();
        this.p = 0;
        byte[] bArr = this.o;
        if (bArr != null) {
            e(bArr);
        }
        byte[] bArr2 = this.s;
        if (bArr2 != null) {
            e(bArr2);
            this.s = null;
        }
        this.r = false;
        if (this.t != null) {
            this.t = null;
            e(this.u);
            this.u = null;
        }
    }

    public void i(byte[] bArr) {
        int i;
        byte[] bArrProcessBlock = this.h.processBlock(bArr, 0, bArr.length);
        int length = bArrProcessBlock.length;
        int i2 = this.m;
        if (length < (i2 + 7) / 8) {
            int i3 = (i2 + 7) / 8;
            byte[] bArr2 = new byte[i3];
            java.lang.System.arraycopy(bArrProcessBlock, 0, bArr2, i3 - bArrProcessBlock.length, bArrProcessBlock.length);
            e(bArrProcessBlock);
            bArrProcessBlock = bArr2;
        }
        if (((bArrProcessBlock[bArrProcessBlock.length - 1] & 255) ^ 188) == 0) {
            i = 1;
        } else {
            i = 2;
            int i4 = ((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 (i4 != iIntValue && (iIntValue != 15052 || i4 != 16588)) {
                throw new java.lang.IllegalStateException("signer initialised with wrong digest for trailer " + i4);
            }
        }
        this.g.doFinal(new byte[this.k], 0);
        int length2 = bArrProcessBlock.length;
        int i5 = this.k;
        byte[] bArrG = g(bArrProcessBlock, (length2 - i5) - i, i5, (bArrProcessBlock.length - i5) - i);
        for (int i6 = 0; i6 != bArrG.length; i6++) {
            bArrProcessBlock[i6] = (byte) (bArrProcessBlock[i6] ^ bArrG[i6]);
        }
        bArrProcessBlock[0] = (byte) (bArrProcessBlock[0] & 127);
        int i7 = 0;
        while (i7 != bArrProcessBlock.length && bArrProcessBlock[i7] != 1) {
            i7++;
        }
        int i8 = i7 + 1;
        if (i8 >= bArrProcessBlock.length) {
            e(bArrProcessBlock);
        }
        this.r = i8 > 1;
        byte[] bArr3 = new byte[(bArrG.length - i8) - this.q];
        this.s = bArr3;
        java.lang.System.arraycopy(bArrProcessBlock, i8, bArr3, 0, bArr3.length);
        byte[] bArr4 = this.s;
        java.lang.System.arraycopy(bArr4, 0, this.o, 0, bArr4.length);
        this.t = bArr;
        this.u = bArrProcessBlock;
        this.v = i8;
        this.w = i;
    }

    @Override // org.bouncycastle.crypto.Signer
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.params.RSAKeyParameters rSAKeyParameters;
        java.security.SecureRandom secureRandomD;
        int length = this.q;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            org.bouncycastle.crypto.params.ParametersWithRandom parametersWithRandom = (org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters;
            rSAKeyParameters = (org.bouncycastle.crypto.params.RSAKeyParameters) parametersWithRandom.a();
            if (z) {
                secureRandomD = parametersWithRandom.b();
                this.i = secureRandomD;
            }
        } else if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithSalt) {
            org.bouncycastle.crypto.params.ParametersWithSalt parametersWithSalt = (org.bouncycastle.crypto.params.ParametersWithSalt) cipherParameters;
            rSAKeyParameters = (org.bouncycastle.crypto.params.RSAKeyParameters) parametersWithSalt.a();
            byte[] bArrB = parametersWithSalt.b();
            this.j = bArrB;
            length = bArrB.length;
            if (bArrB.length != this.q) {
                throw new java.lang.IllegalArgumentException("Fixed salt is of wrong length");
            }
        } else {
            rSAKeyParameters = (org.bouncycastle.crypto.params.RSAKeyParameters) cipherParameters;
            if (z) {
                secureRandomD = org.bouncycastle.crypto.CryptoServicesRegistrar.d();
                this.i = secureRandomD;
            }
        }
        this.h.init(z, rSAKeyParameters);
        int iBitLength = rSAKeyParameters.i().bitLength();
        this.m = iBitLength;
        byte[] bArr = new byte[(iBitLength + 7) / 8];
        this.n = bArr;
        int i = this.l;
        int length2 = bArr.length;
        if (i == 188) {
            this.o = new byte[((length2 - this.g.getDigestSize()) - length) - 2];
        } else {
            this.o = new byte[((length2 - this.g.getDigestSize()) - length) - 3];
        }
        h();
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte b) {
        if (this.t == null) {
            int i = this.p;
            byte[] bArr = this.o;
            if (i < bArr.length) {
                this.p = i + 1;
                bArr[i] = b;
                return;
            }
        }
        this.g.update(b);
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte[] bArr, int i, int i2) {
        if (this.t == null) {
            while (i2 > 0 && this.p < this.o.length) {
                update(bArr[i]);
                i++;
                i2--;
            }
        }
        if (i2 > 0) {
            this.g.update(bArr, i, i2);
        }
    }
}
