package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class GCMBlockCipher implements org.bouncycastle.crypto.modes.GCMModeCipher {
    private org.bouncycastle.crypto.BlockCipher a;
    private org.bouncycastle.crypto.modes.gcm.GCMMultiplier b;
    private org.bouncycastle.crypto.modes.gcm.GCMExponentiator c;
    private boolean d;
    private boolean e;
    private int f;
    private byte[] g;
    private byte[] h;
    private byte[] i;
    private byte[] j;
    private byte[] k;
    private byte[] l;
    private byte[] m;
    private byte[] n;
    private byte[] o;
    private byte[] p;
    private byte[] q;
    private int r;
    private int s;
    private long t;
    private byte[] u;
    private int v;
    private long w;
    private long x;

    public GCMBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this(blockCipher, null);
    }

    private void a() {
        if (this.e) {
            return;
        }
        if (!this.d) {
            throw new java.lang.IllegalStateException("GCM cipher needs to be initialised");
        }
        throw new java.lang.IllegalStateException("GCM cipher cannot be reused for encryption");
    }

    private void e(byte[] bArr, int i, byte[] bArr2, int i2) {
        if (bArr2.length - i2 < 16) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
        }
        if (this.t == 0) {
            l();
        }
        byte[] bArr3 = new byte[16];
        k(bArr3);
        i(this.n, bArr, i);
        org.bouncycastle.crypto.modes.gcm.GCMUtil.r(bArr3, 0, bArr, i, bArr2, i2);
        this.t += 16;
    }

    private void f(byte[] bArr, int i, byte[] bArr2, int i2) {
        if (bArr2.length - i2 < 16) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
        }
        if (this.t == 0) {
            l();
        }
        byte[] bArr3 = new byte[16];
        k(bArr3);
        org.bouncycastle.crypto.modes.gcm.GCMUtil.t(bArr3, bArr, i);
        h(this.n, bArr3);
        java.lang.System.arraycopy(bArr3, 0, bArr2, i2, 16);
        this.t += 16;
    }

    private void g(byte[] bArr, byte[] bArr2, int i) {
        for (int i2 = 0; i2 < i; i2 += 16) {
            j(bArr, bArr2, i2, java.lang.Math.min(i - i2, 16));
        }
    }

    private void h(byte[] bArr, byte[] bArr2) {
        org.bouncycastle.crypto.modes.gcm.GCMUtil.s(bArr, bArr2);
        this.b.b(bArr);
    }

    private void i(byte[] bArr, byte[] bArr2, int i) {
        org.bouncycastle.crypto.modes.gcm.GCMUtil.t(bArr, bArr2, i);
        this.b.b(bArr);
    }

    private void j(byte[] bArr, byte[] bArr2, int i, int i2) {
        org.bouncycastle.crypto.modes.gcm.GCMUtil.u(bArr, bArr2, i, i2);
        this.b.b(bArr);
    }

    private void k(byte[] bArr) {
        int i = this.r;
        if (i == 0) {
            throw new java.lang.IllegalStateException("Attempt to process too many blocks");
        }
        this.r = i - 1;
        byte[] bArr2 = this.q;
        int i2 = (bArr2[15] & 255) + 1;
        bArr2[15] = (byte) i2;
        int i3 = (i2 >>> 8) + (bArr2[14] & 255);
        bArr2[14] = (byte) i3;
        int i4 = (i3 >>> 8) + (bArr2[13] & 255);
        bArr2[13] = (byte) i4;
        bArr2[12] = (byte) ((i4 >>> 8) + (bArr2[12] & 255));
        this.a.c(bArr2, 0, bArr, 0);
    }

    private void l() {
        if (this.w > 0) {
            java.lang.System.arraycopy(this.o, 0, this.p, 0, 16);
            this.x = this.w;
        }
        int i = this.v;
        if (i > 0) {
            j(this.p, this.u, 0, i);
            this.x += (long) this.v;
        }
        if (this.x > 0) {
            java.lang.System.arraycopy(this.p, 0, this.n, 0, 16);
        }
    }

    public static org.bouncycastle.crypto.modes.GCMModeCipher m(org.bouncycastle.crypto.BlockCipher blockCipher) {
        return new org.bouncycastle.crypto.modes.GCMBlockCipher(blockCipher);
    }

    private void n(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        byte[] bArr3 = new byte[16];
        k(bArr3);
        if (this.d) {
            org.bouncycastle.crypto.modes.gcm.GCMUtil.q(bArr, i, bArr3, 0, i2);
            j(this.n, bArr, i, i2);
        } else {
            j(this.n, bArr, i, i2);
            org.bouncycastle.crypto.modes.gcm.GCMUtil.q(bArr, i, bArr3, 0, i2);
        }
        java.lang.System.arraycopy(bArr, i, bArr2, i3, i2);
        this.t += (long) i2;
    }

    private void o(boolean z) {
        this.a.reset();
        this.n = new byte[16];
        this.o = new byte[16];
        this.p = new byte[16];
        this.u = new byte[16];
        this.v = 0;
        this.w = 0L;
        this.x = 0L;
        this.q = org.bouncycastle.util.Arrays.j(this.k);
        this.r = -2;
        this.s = 0;
        this.t = 0L;
        byte[] bArr = this.l;
        if (bArr != null) {
            org.bouncycastle.util.Arrays.G(bArr, (byte) 0);
        }
        if (z) {
            this.m = null;
        }
        if (this.d) {
            this.e = false;
            return;
        }
        byte[] bArr2 = this.i;
        if (bArr2 != null) {
            c(bArr2, 0, bArr2.length);
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public byte[] b() {
        byte[] bArr = this.m;
        return bArr == null ? new byte[this.f] : org.bouncycastle.util.Arrays.j(bArr);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void c(byte[] bArr, int i, int i2) {
        a();
        int i3 = this.v;
        if (i3 > 0) {
            int i4 = 16 - i3;
            if (i2 < i4) {
                java.lang.System.arraycopy(bArr, i, this.u, i3, i2);
                this.v += i2;
                return;
            } else {
                java.lang.System.arraycopy(bArr, i, this.u, i3, i4);
                h(this.o, this.u);
                this.w += 16;
                i += i4;
                i2 -= i4;
            }
        }
        int i5 = i2 + i;
        int i6 = i5 - 16;
        while (i <= i6) {
            i(this.o, bArr, i);
            this.w += 16;
            i += 16;
        }
        int i7 = i5 - i;
        this.v = i7;
        java.lang.System.arraycopy(bArr, i, this.u, 0, i7);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void d(byte b) {
        a();
        byte[] bArr = this.u;
        int i = this.v;
        bArr[i] = b;
        int i2 = i + 1;
        this.v = i2;
        if (i2 == 16) {
            h(this.o, bArr);
            this.v = 0;
            this.w += 16;
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int doFinal(byte[] bArr, int i) throws org.bouncycastle.crypto.InvalidCipherTextException {
        org.bouncycastle.crypto.modes.GCMBlockCipher gCMBlockCipher;
        byte[] bArr2;
        int i2;
        a();
        if (this.t == 0) {
            l();
        }
        int i3 = this.s;
        if (!this.d) {
            int i4 = this.f;
            if (i3 < i4) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("data too short");
            }
            i3 -= i4;
            if (bArr.length - i < i3) {
                throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
            }
        } else if (bArr.length - i < this.f + i3) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
        }
        int i5 = i3;
        if (i5 > 0) {
            gCMBlockCipher = this;
            bArr2 = bArr;
            i2 = i;
            gCMBlockCipher.n(this.l, 0, i5, bArr2, i2);
        } else {
            gCMBlockCipher = this;
            bArr2 = bArr;
            i2 = i;
        }
        long j = gCMBlockCipher.w;
        int i6 = gCMBlockCipher.v;
        long j2 = j + ((long) i6);
        gCMBlockCipher.w = j2;
        if (j2 > gCMBlockCipher.x) {
            if (i6 > 0) {
                gCMBlockCipher.j(gCMBlockCipher.o, gCMBlockCipher.u, 0, i6);
            }
            if (gCMBlockCipher.x > 0) {
                org.bouncycastle.crypto.modes.gcm.GCMUtil.s(gCMBlockCipher.o, gCMBlockCipher.p);
            }
            long j3 = ((gCMBlockCipher.t * 8) + 127) >>> 7;
            byte[] bArr3 = new byte[16];
            if (gCMBlockCipher.c == null) {
                org.bouncycastle.crypto.modes.gcm.BasicGCMExponentiator basicGCMExponentiator = new org.bouncycastle.crypto.modes.gcm.BasicGCMExponentiator();
                gCMBlockCipher.c = basicGCMExponentiator;
                basicGCMExponentiator.a(gCMBlockCipher.j);
            }
            gCMBlockCipher.c.b(j3, bArr3);
            org.bouncycastle.crypto.modes.gcm.GCMUtil.j(gCMBlockCipher.o, bArr3);
            org.bouncycastle.crypto.modes.gcm.GCMUtil.s(gCMBlockCipher.n, gCMBlockCipher.o);
        }
        byte[] bArr4 = new byte[16];
        org.bouncycastle.util.Pack.C(gCMBlockCipher.w * 8, bArr4, 0);
        org.bouncycastle.util.Pack.C(gCMBlockCipher.t * 8, bArr4, 8);
        gCMBlockCipher.h(gCMBlockCipher.n, bArr4);
        byte[] bArr5 = new byte[16];
        gCMBlockCipher.a.c(gCMBlockCipher.k, 0, bArr5, 0);
        org.bouncycastle.crypto.modes.gcm.GCMUtil.s(bArr5, gCMBlockCipher.n);
        int i7 = gCMBlockCipher.f;
        byte[] bArr6 = new byte[i7];
        gCMBlockCipher.m = bArr6;
        java.lang.System.arraycopy(bArr5, 0, bArr6, 0, i7);
        if (gCMBlockCipher.d) {
            java.lang.System.arraycopy(gCMBlockCipher.m, 0, bArr2, i2 + gCMBlockCipher.s, gCMBlockCipher.f);
            i5 += gCMBlockCipher.f;
        } else {
            int i8 = gCMBlockCipher.f;
            byte[] bArr7 = new byte[i8];
            java.lang.System.arraycopy(gCMBlockCipher.l, i5, bArr7, 0, i8);
            if (!org.bouncycastle.util.Arrays.z(gCMBlockCipher.m, bArr7)) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("mac check in GCM failed");
            }
        }
        gCMBlockCipher.o(false);
        return i5;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public java.lang.String getAlgorithmName() {
        return this.a.getAlgorithmName() + "/GCM";
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getOutputSize(int i) {
        int i2 = i + this.s;
        boolean z = this.d;
        int i3 = this.f;
        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.a;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getUpdateOutputSize(int i) {
        int i2 = i + this.s;
        if (!this.d) {
            int i3 = this.f;
            if (i2 < i3) {
                return 0;
            }
            i2 -= i3;
        }
        return i2 - (i2 % 16);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        byte[] bArrA;
        org.bouncycastle.crypto.params.KeyParameter keyParameterB;
        byte[] bArr;
        this.d = z;
        this.m = null;
        this.e = true;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.AEADParameters) {
            org.bouncycastle.crypto.params.AEADParameters aEADParameters = (org.bouncycastle.crypto.params.AEADParameters) cipherParameters;
            bArrA = aEADParameters.d();
            this.i = aEADParameters.a();
            int iC = aEADParameters.c();
            if (iC < 32 || iC > 128 || iC % 8 != 0) {
                throw new java.lang.IllegalArgumentException("Invalid value for MAC size: " + iC);
            }
            this.f = iC / 8;
            keyParameterB = aEADParameters.b();
        } else {
            if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
                throw new java.lang.IllegalArgumentException("invalid parameters passed to GCM");
            }
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            bArrA = parametersWithIV.a();
            this.i = null;
            this.f = 16;
            keyParameterB = (org.bouncycastle.crypto.params.KeyParameter) parametersWithIV.b();
        }
        this.l = new byte[z ? 16 : this.f + 16];
        if (bArrA == null || bArrA.length < 1) {
            throw new java.lang.IllegalArgumentException("IV must be at least 1 byte");
        }
        if (z && (bArr = this.h) != null && org.bouncycastle.util.Arrays.d(bArr, bArrA)) {
            if (keyParameterB == null) {
                throw new java.lang.IllegalArgumentException("cannot reuse nonce for GCM encryption");
            }
            byte[] bArr2 = this.g;
            if (bArr2 != null && org.bouncycastle.util.Arrays.d(bArr2, keyParameterB.b())) {
                throw new java.lang.IllegalArgumentException("cannot reuse nonce for GCM encryption");
            }
        }
        this.h = bArrA;
        if (keyParameterB != null) {
            this.g = keyParameterB.b();
        }
        if (keyParameterB != null) {
            this.a.init(true, keyParameterB);
            byte[] bArr3 = new byte[16];
            this.j = bArr3;
            this.a.c(bArr3, 0, bArr3, 0);
            this.b.a(this.j);
            this.c = null;
        } else if (this.j == null) {
            throw new java.lang.IllegalArgumentException("Key must be specified in initial init");
        }
        byte[] bArr4 = new byte[16];
        this.k = bArr4;
        byte[] bArr5 = this.h;
        if (bArr5.length == 12) {
            java.lang.System.arraycopy(bArr5, 0, bArr4, 0, bArr5.length);
            this.k[15] = 1;
        } else {
            g(bArr4, bArr5, bArr5.length);
            byte[] bArr6 = new byte[16];
            org.bouncycastle.util.Pack.C(((long) this.h.length) * 8, bArr6, 8);
            h(this.k, bArr6);
        }
        this.n = new byte[16];
        this.o = new byte[16];
        this.p = new byte[16];
        this.u = new byte[16];
        this.v = 0;
        this.w = 0L;
        this.x = 0L;
        this.q = org.bouncycastle.util.Arrays.j(this.k);
        this.r = -2;
        this.s = 0;
        this.t = 0L;
        byte[] bArr7 = this.i;
        if (bArr7 != null) {
            c(bArr7, 0, bArr7.length);
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processByte(byte b, byte[] bArr, int i) {
        a();
        byte[] bArr2 = this.l;
        int i2 = this.s;
        bArr2[i2] = b;
        int i3 = i2 + 1;
        this.s = i3;
        if (i3 != bArr2.length) {
            return 0;
        }
        if (this.d) {
            f(bArr2, 0, bArr, i);
            this.s = 0;
        } else {
            e(bArr2, 0, bArr, i);
            byte[] bArr3 = this.l;
            java.lang.System.arraycopy(bArr3, 16, bArr3, 0, this.f);
            this.s = this.f;
        }
        return 16;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        int i4;
        a();
        if (bArr.length - i < i2) {
            throw new org.bouncycastle.crypto.DataLengthException("Input buffer too short");
        }
        int i5 = 16;
        if (this.d) {
            int i6 = this.s;
            if (i6 > 0) {
                int i7 = 16 - i6;
                if (i2 < i7) {
                    java.lang.System.arraycopy(bArr, i, this.l, i6, i2);
                } else {
                    java.lang.System.arraycopy(bArr, i, this.l, i6, i7);
                    f(this.l, 0, bArr2, i3);
                    i += i7;
                    i2 -= i7;
                }
            } else {
                i5 = 0;
            }
            int i8 = i2 + i;
            int i9 = i8 - 16;
            while (i <= i9) {
                f(bArr, i, bArr2, i3 + i5);
                i += 16;
                i5 += 16;
            }
            int i10 = i8 - i;
            this.s = i10;
            java.lang.System.arraycopy(bArr, i, this.l, 0, i10);
            return i5;
        }
        byte[] bArr3 = this.l;
        int length = bArr3.length;
        int i11 = this.s;
        int i12 = length - i11;
        if (i2 >= i12) {
            if (i11 >= 16) {
                e(bArr3, 0, bArr2, i3);
                byte[] bArr4 = this.l;
                int i13 = this.s - 16;
                this.s = i13;
                java.lang.System.arraycopy(bArr4, 16, bArr4, 0, i13);
                if (i2 < i12 + 16) {
                    java.lang.System.arraycopy(bArr, i, this.l, this.s, i2);
                    this.s += i2;
                    return 16;
                }
                i4 = 16;
            } else {
                i4 = 0;
            }
            byte[] bArr5 = this.l;
            int length2 = (i2 + i) - bArr5.length;
            int i14 = this.s;
            int i15 = 16 - i14;
            java.lang.System.arraycopy(bArr, i, bArr5, i14, i15);
            e(this.l, 0, bArr2, i3 + i4);
            int i16 = i + i15;
            int i17 = i4 + 16;
            while (i16 <= length2) {
                e(bArr, i16, bArr2, i3 + i17);
                i16 += 16;
                i17 += 16;
            }
            byte[] bArr6 = this.l;
            int length3 = (bArr6.length + length2) - i16;
            this.s = length3;
            java.lang.System.arraycopy(bArr, i16, bArr6, 0, length3);
            return i17;
        }
        java.lang.System.arraycopy(bArr, i, bArr3, i11, i2);
        this.s += i2;
        return 0;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void reset() {
        o(true);
    }

    public GCMBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher, org.bouncycastle.crypto.modes.gcm.GCMMultiplier gCMMultiplier) {
        if (blockCipher.b() != 16) {
            throw new java.lang.IllegalArgumentException("cipher required with a block size of 16.");
        }
        gCMMultiplier = gCMMultiplier == null ? new org.bouncycastle.crypto.modes.gcm.Tables4kGCMMultiplier() : gCMMultiplier;
        this.a = blockCipher;
        this.b = gCMMultiplier;
    }
}
