package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class OCBBlockCipher implements org.bouncycastle.crypto.modes.AEADBlockCipher {
    private org.bouncycastle.crypto.BlockCipher a;
    private org.bouncycastle.crypto.BlockCipher b;
    private boolean c;
    private int d;
    private byte[] e;
    private java.util.Vector f;
    private byte[] g;
    private byte[] h;
    private byte[] l;
    private byte[] m;
    private int n;
    private int o;
    private long p;
    private long q;
    private byte[] r;
    private byte[] s;
    private byte[] u;
    private byte[] v;
    private byte[] i = null;
    private byte[] j = new byte[24];
    private byte[] k = new byte[16];
    private byte[] t = new byte[16];

    public OCBBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher, org.bouncycastle.crypto.BlockCipher blockCipher2) {
        if (blockCipher == null) {
            throw new java.lang.IllegalArgumentException("'hashCipher' cannot be null");
        }
        if (blockCipher.b() != 16) {
            throw new java.lang.IllegalArgumentException("'hashCipher' must have a block size of 16");
        }
        if (blockCipher2 == null) {
            throw new java.lang.IllegalArgumentException("'mainCipher' cannot be null");
        }
        if (blockCipher2.b() != 16) {
            throw new java.lang.IllegalArgumentException("'mainCipher' must have a block size of 16");
        }
        if (!blockCipher.getAlgorithmName().equals(blockCipher2.getAlgorithmName())) {
            throw new java.lang.IllegalArgumentException("'hashCipher' and 'mainCipher' must be the same algorithm");
        }
        this.a = blockCipher;
        this.b = blockCipher2;
    }

    protected static byte[] a(byte[] bArr) {
        byte[] bArr2 = new byte[16];
        bArr2[15] = (byte) ((135 >>> ((1 - m(bArr, bArr2)) << 3)) ^ bArr2[15]);
        return bArr2;
    }

    protected static void e(byte[] bArr, int i) {
        bArr[i] = -128;
        while (true) {
            i++;
            if (i >= 16) {
                return;
            } else {
                bArr[i] = 0;
            }
        }
    }

    protected static int f(long j) {
        return org.bouncycastle.util.Longs.a(j);
    }

    protected static int m(byte[] bArr, byte[] bArr2) {
        int i = 16;
        int i2 = 0;
        while (true) {
            i--;
            if (i < 0) {
                return i2;
            }
            int i3 = bArr[i] & 255;
            bArr2[i] = (byte) (i2 | (i3 << 1));
            i2 = (i3 >>> 7) & 1;
        }
    }

    protected static void o(byte[] bArr, byte[] bArr2) {
        org.bouncycastle.util.Bytes.c(16, bArr2, bArr);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public byte[] b() {
        byte[] bArr = this.v;
        return bArr == null ? new byte[this.d] : org.bouncycastle.util.Arrays.j(bArr);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void c(byte[] bArr, int i, int i2) {
        for (int i3 = 0; i3 < i2; i3++) {
            byte[] bArr2 = this.l;
            int i4 = this.n;
            bArr2[i4] = bArr[i + i3];
            int i5 = i4 + 1;
            this.n = i5;
            if (i5 == bArr2.length) {
                i();
            }
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void d(byte b) {
        byte[] bArr = this.l;
        int i = this.n;
        bArr[i] = b;
        int i2 = i + 1;
        this.n = i2;
        if (i2 == bArr.length) {
            i();
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int doFinal(byte[] bArr, int i) throws org.bouncycastle.crypto.InvalidCipherTextException {
        byte[] bArr2;
        if (this.c) {
            bArr2 = null;
        } else {
            int i2 = this.o;
            int i3 = this.d;
            if (i2 < i3) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("data too short");
            }
            int i4 = i2 - i3;
            this.o = i4;
            bArr2 = new byte[i3];
            java.lang.System.arraycopy(this.m, i4, bArr2, 0, i3);
        }
        int i5 = this.n;
        if (i5 > 0) {
            e(this.l, i5);
            n(this.g);
        }
        int i6 = this.o;
        if (i6 > 0) {
            if (this.c) {
                e(this.m, i6);
                o(this.u, this.m);
            }
            o(this.t, this.g);
            byte[] bArr3 = new byte[16];
            this.a.c(this.t, 0, bArr3, 0);
            o(this.m, bArr3);
            int length = bArr.length;
            int i7 = this.o;
            if (length < i + i7) {
                throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
            }
            java.lang.System.arraycopy(this.m, 0, bArr, i, i7);
            if (!this.c) {
                e(this.m, this.o);
                o(this.u, this.m);
            }
        }
        o(this.u, this.t);
        o(this.u, this.h);
        org.bouncycastle.crypto.BlockCipher blockCipher = this.a;
        byte[] bArr4 = this.u;
        blockCipher.c(bArr4, 0, bArr4, 0);
        o(this.u, this.s);
        int i8 = this.d;
        byte[] bArr5 = new byte[i8];
        this.v = bArr5;
        java.lang.System.arraycopy(this.u, 0, bArr5, 0, i8);
        int i9 = this.o;
        if (this.c) {
            int length2 = bArr.length;
            int i10 = i + i9;
            int i11 = this.d;
            if (length2 < i10 + i11) {
                throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
            }
            java.lang.System.arraycopy(this.v, 0, bArr, i10, i11);
            i9 += this.d;
        } else if (!org.bouncycastle.util.Arrays.z(this.v, bArr2)) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("mac check in OCB failed");
        }
        l(false);
        return i9;
    }

    protected void g(byte[] bArr) {
        if (bArr != null) {
            org.bouncycastle.util.Arrays.G(bArr, (byte) 0);
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public java.lang.String getAlgorithmName() {
        return this.b.getAlgorithmName() + "/OCB";
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getOutputSize(int i) {
        int i2 = i + this.o;
        boolean z = this.c;
        int i3 = this.d;
        if (z) {
            return i2 + i3;
        }
        if (i2 < i3) {
            return 0;
        }
        return i2 - i3;
    }

    @Override // org.bouncycastle.crypto.modes.AEADBlockCipher
    public org.bouncycastle.crypto.BlockCipher getUnderlyingCipher() {
        return this.b;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getUpdateOutputSize(int i) {
        int i2 = i + this.o;
        if (!this.c) {
            int i3 = this.d;
            if (i2 < i3) {
                return 0;
            }
            i2 -= i3;
        }
        return i2 - (i2 % 16);
    }

    protected byte[] h(int i) {
        while (i >= this.f.size()) {
            java.util.Vector vector = this.f;
            vector.addElement(a((byte[]) vector.lastElement()));
        }
        return (byte[]) this.f.elementAt(i);
    }

    protected void i() {
        long j = this.p + 1;
        this.p = j;
        n(h(f(j)));
        this.n = 0;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        byte[] bArrA;
        org.bouncycastle.crypto.params.KeyParameter keyParameterB;
        boolean z2 = this.c;
        this.c = z;
        this.v = null;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.AEADParameters) {
            org.bouncycastle.crypto.params.AEADParameters aEADParameters = (org.bouncycastle.crypto.params.AEADParameters) cipherParameters;
            bArrA = aEADParameters.d();
            this.e = aEADParameters.a();
            int iC = aEADParameters.c();
            if (iC < 64 || iC > 128 || iC % 8 != 0) {
                throw new java.lang.IllegalArgumentException("Invalid value for MAC size: " + iC);
            }
            this.d = iC / 8;
            keyParameterB = aEADParameters.b();
        } else {
            if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
                throw new java.lang.IllegalArgumentException("invalid parameters passed to OCB");
            }
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            bArrA = parametersWithIV.a();
            this.e = null;
            this.d = 16;
            keyParameterB = (org.bouncycastle.crypto.params.KeyParameter) parametersWithIV.b();
        }
        this.l = new byte[16];
        this.m = new byte[z ? 16 : this.d + 16];
        if (bArrA == null) {
            bArrA = new byte[0];
        }
        if (bArrA.length > 15) {
            throw new java.lang.IllegalArgumentException("IV must be no more than 15 bytes");
        }
        if (keyParameterB != null) {
            this.a.init(true, keyParameterB);
            this.b.init(z, keyParameterB);
            this.i = null;
        } else if (z2 != z) {
            throw new java.lang.IllegalArgumentException("cannot change encrypting state without providing key.");
        }
        byte[] bArr = new byte[16];
        this.g = bArr;
        this.a.c(bArr, 0, bArr, 0);
        this.h = a(this.g);
        java.util.Vector vector = new java.util.Vector();
        this.f = vector;
        vector.addElement(a(this.h));
        int iK = k(bArrA);
        int i = iK % 8;
        int i2 = iK / 8;
        if (i == 0) {
            java.lang.System.arraycopy(this.j, i2, this.k, 0, 16);
        } else {
            for (int i3 = 0; i3 < 16; i3++) {
                byte[] bArr2 = this.j;
                int i4 = bArr2[i2] & 255;
                i2++;
                this.k[i3] = (byte) (((bArr2[i2] & 255) >>> (8 - i)) | (i4 << i));
            }
        }
        this.n = 0;
        this.o = 0;
        this.p = 0L;
        this.q = 0L;
        this.r = new byte[16];
        this.s = new byte[16];
        java.lang.System.arraycopy(this.k, 0, this.t, 0, 16);
        this.u = new byte[16];
        byte[] bArr3 = this.e;
        if (bArr3 != null) {
            c(bArr3, 0, bArr3.length);
        }
    }

    protected void j(byte[] bArr, int i) {
        if (bArr.length < i + 16) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
        }
        if (this.c) {
            o(this.u, this.m);
            this.o = 0;
        }
        byte[] bArr2 = this.t;
        long j = this.q + 1;
        this.q = j;
        o(bArr2, h(f(j)));
        o(this.m, this.t);
        org.bouncycastle.crypto.BlockCipher blockCipher = this.b;
        byte[] bArr3 = this.m;
        blockCipher.c(bArr3, 0, bArr3, 0);
        o(this.m, this.t);
        java.lang.System.arraycopy(this.m, 0, bArr, i, 16);
        if (this.c) {
            return;
        }
        o(this.u, this.m);
        byte[] bArr4 = this.m;
        java.lang.System.arraycopy(bArr4, 16, bArr4, 0, this.d);
        this.o = this.d;
    }

    protected int k(byte[] bArr) {
        byte[] bArr2 = new byte[16];
        int i = 0;
        java.lang.System.arraycopy(bArr, 0, bArr2, 16 - bArr.length, bArr.length);
        bArr2[0] = (byte) (this.d << 4);
        int length = 15 - bArr.length;
        bArr2[length] = (byte) (bArr2[length] | 1);
        byte b = bArr2[15];
        int i2 = b & 63;
        bArr2[15] = (byte) (b & 192);
        byte[] bArr3 = this.i;
        if (bArr3 == null || !org.bouncycastle.util.Arrays.d(bArr2, bArr3)) {
            byte[] bArr4 = new byte[16];
            this.i = bArr2;
            this.a.c(bArr2, 0, bArr4, 0);
            java.lang.System.arraycopy(bArr4, 0, this.j, 0, 16);
            while (i < 8) {
                byte[] bArr5 = this.j;
                int i3 = i + 16;
                byte b2 = bArr4[i];
                i++;
                bArr5[i3] = (byte) (b2 ^ bArr4[i]);
            }
        }
        return i2;
    }

    protected void l(boolean z) {
        this.a.reset();
        this.b.reset();
        g(this.l);
        g(this.m);
        this.n = 0;
        this.o = 0;
        this.p = 0L;
        this.q = 0L;
        g(this.r);
        g(this.s);
        java.lang.System.arraycopy(this.k, 0, this.t, 0, 16);
        g(this.u);
        if (z) {
            this.v = null;
        }
        byte[] bArr = this.e;
        if (bArr != null) {
            c(bArr, 0, bArr.length);
        }
    }

    protected void n(byte[] bArr) {
        o(this.r, bArr);
        o(this.l, this.r);
        org.bouncycastle.crypto.BlockCipher blockCipher = this.a;
        byte[] bArr2 = this.l;
        blockCipher.c(bArr2, 0, bArr2, 0);
        o(this.s, this.l);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processByte(byte b, byte[] bArr, int i) {
        byte[] bArr2 = this.m;
        int i2 = this.o;
        bArr2[i2] = b;
        int i3 = i2 + 1;
        this.o = i3;
        if (i3 != bArr2.length) {
            return 0;
        }
        j(bArr, i);
        return 16;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        if (bArr.length < i + i2) {
            throw new org.bouncycastle.crypto.DataLengthException("Input buffer too short");
        }
        int i4 = 0;
        for (int i5 = 0; i5 < i2; i5++) {
            byte[] bArr3 = this.m;
            int i6 = this.o;
            bArr3[i6] = bArr[i + i5];
            int i7 = i6 + 1;
            this.o = i7;
            if (i7 == bArr3.length) {
                j(bArr2, i3 + i4);
                i4 += 16;
            }
        }
        return i4;
    }
}
