package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class GCMSIVBlockCipher implements org.bouncycastle.crypto.modes.AEADBlockCipher {
    private final org.bouncycastle.crypto.BlockCipher a;
    private final org.bouncycastle.crypto.modes.gcm.GCMMultiplier b;
    private final byte[] c;
    private final byte[] d;
    private final org.bouncycastle.crypto.modes.GCMSIVBlockCipher.GCMSIVHasher e;
    private final org.bouncycastle.crypto.modes.GCMSIVBlockCipher.GCMSIVHasher f;
    private org.bouncycastle.crypto.modes.GCMSIVBlockCipher.GCMSIVCache g;
    private org.bouncycastle.crypto.modes.GCMSIVBlockCipher.GCMSIVCache h;
    private boolean i;
    private byte[] j;
    private byte[] k;
    private int l;
    private byte[] m;

    private static class GCMSIVCache extends java.io.ByteArrayOutputStream {
        GCMSIVCache() {
        }

        void a() {
            org.bouncycastle.util.Arrays.G(d(), (byte) 0);
        }

        byte[] d() {
            return ((java.io.ByteArrayOutputStream) this).buf;
        }
    }

    private class GCMSIVHasher {
        private final byte[] a;
        private final byte[] b;
        private int c;
        private long d;

        private GCMSIVHasher() {
            this.a = new byte[16];
            this.b = new byte[1];
        }

        void a() {
            if (this.c > 0) {
                org.bouncycastle.util.Arrays.G(org.bouncycastle.crypto.modes.GCMSIVBlockCipher.this.d, (byte) 0);
                org.bouncycastle.crypto.modes.GCMSIVBlockCipher.p(this.a, 0, this.c, org.bouncycastle.crypto.modes.GCMSIVBlockCipher.this.d);
                org.bouncycastle.crypto.modes.GCMSIVBlockCipher gCMSIVBlockCipher = org.bouncycastle.crypto.modes.GCMSIVBlockCipher.this;
                gCMSIVBlockCipher.q(gCMSIVBlockCipher.d);
            }
        }

        long b() {
            return this.d;
        }

        void c() {
            this.c = 0;
            this.d = 0L;
        }

        void d(byte b) {
            byte[] bArr = this.b;
            bArr[0] = b;
            e(bArr, 0, 1);
        }

        void e(byte[] bArr, int i, int i2) {
            int i3;
            int i4 = this.c;
            int i5 = 16 - i4;
            if (i4 <= 0 || i2 < i5) {
                i3 = i2;
                i5 = 0;
            } else {
                java.lang.System.arraycopy(bArr, i, this.a, i4, i5);
                org.bouncycastle.crypto.modes.GCMSIVBlockCipher.p(this.a, 0, 16, org.bouncycastle.crypto.modes.GCMSIVBlockCipher.this.d);
                org.bouncycastle.crypto.modes.GCMSIVBlockCipher gCMSIVBlockCipher = org.bouncycastle.crypto.modes.GCMSIVBlockCipher.this;
                gCMSIVBlockCipher.q(gCMSIVBlockCipher.d);
                i3 = i2 - i5;
                this.c = 0;
            }
            while (i3 >= 16) {
                org.bouncycastle.crypto.modes.GCMSIVBlockCipher.p(bArr, i + i5, 16, org.bouncycastle.crypto.modes.GCMSIVBlockCipher.this.d);
                org.bouncycastle.crypto.modes.GCMSIVBlockCipher gCMSIVBlockCipher2 = org.bouncycastle.crypto.modes.GCMSIVBlockCipher.this;
                gCMSIVBlockCipher2.q(gCMSIVBlockCipher2.d);
                i5 += 16;
                i3 -= 16;
            }
            if (i3 > 0) {
                java.lang.System.arraycopy(bArr, i + i5, this.a, this.c, i3);
                this.c += i3;
            }
            this.d += (long) i2;
        }
    }

    public GCMSIVBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this(blockCipher, new org.bouncycastle.crypto.modes.gcm.Tables4kGCMMultiplier());
    }

    private static int g(byte[] bArr) {
        if (bArr == null) {
            return 0;
        }
        return bArr.length;
    }

    private byte[] h() {
        this.f.a();
        byte[] bArrL = l();
        byte[] bArr = new byte[16];
        for (int i = 0; i < 12; i++) {
            bArrL[i] = (byte) (bArrL[i] ^ this.k[i]);
        }
        bArrL[15] = (byte) (bArrL[15] & (-129));
        this.a.c(bArrL, 0, bArr, 0);
        return bArr;
    }

    private void i(int i) {
        int i2 = this.l;
        if ((i2 & 1) == 0) {
            throw new java.lang.IllegalStateException("Cipher is not initialised");
        }
        if ((i2 & 2) != 0) {
            throw new java.lang.IllegalStateException("AEAD data cannot be processed after ordinary data");
        }
        if (this.e.b() - Long.MIN_VALUE > ((long) (2147483623 - i)) - Long.MIN_VALUE) {
            throw new java.lang.IllegalStateException("AEAD byte count exceeded");
        }
    }

    private static void j(byte[] bArr, int i, int i2, boolean z) {
        int iG = g(bArr);
        int i3 = i + i2;
        if (i2 < 0 || i < 0 || i3 < 0 || i3 > iG) {
            if (!z) {
                throw new org.bouncycastle.crypto.DataLengthException("Input buffer too short.");
            }
        }
    }

    private void k(int i) {
        long j;
        int i2 = this.l;
        if ((i2 & 1) == 0) {
            throw new java.lang.IllegalStateException("Cipher is not initialised");
        }
        if ((i2 & 2) == 0) {
            this.e.a();
            this.l |= 2;
        }
        long size = this.g.size();
        if (this.i) {
            j = 2147483623;
        } else {
            size = this.h.size();
            j = 2147483639;
        }
        if (size - Long.MIN_VALUE > (j - ((long) i)) - Long.MIN_VALUE) {
            throw new java.lang.IllegalStateException("byte count exceeded");
        }
    }

    private byte[] l() {
        byte[] bArr = new byte[16];
        r();
        p(this.c, 0, 16, bArr);
        return bArr;
    }

    private void m() throws org.bouncycastle.crypto.InvalidCipherTextException, java.io.IOException {
        byte[] bArrD = this.h.d();
        int size = this.h.size();
        int i = size - 16;
        if (i < 0) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("Data too short");
        }
        byte[] bArrD2 = org.bouncycastle.util.Arrays.D(bArrD, i, size);
        byte[] bArrJ = org.bouncycastle.util.Arrays.j(bArrD2);
        bArrJ[15] = (byte) (bArrJ[15] | (-128));
        byte[] bArr = new byte[16];
        int i2 = 0;
        while (i > 0) {
            this.a.c(bArrJ, 0, bArr, 0);
            int iMin = java.lang.Math.min(16, i);
            w(bArr, bArrD, i2, iMin);
            this.g.write(bArr, 0, iMin);
            this.f.e(bArr, 0, iMin);
            i -= iMin;
            i2 += iMin;
            s(bArrJ);
        }
        byte[] bArrH = h();
        if (!org.bouncycastle.util.Arrays.z(bArrH, bArrD2)) {
            reset();
            throw new org.bouncycastle.crypto.InvalidCipherTextException("mac check failed");
        }
        byte[] bArr2 = this.m;
        java.lang.System.arraycopy(bArrH, 0, bArr2, 0, bArr2.length);
    }

    private void n(org.bouncycastle.crypto.params.KeyParameter keyParameter) {
        byte[] bArr = new byte[16];
        byte[] bArr2 = new byte[16];
        byte[] bArr3 = new byte[16];
        int iC = keyParameter.c();
        byte[] bArr4 = new byte[iC];
        java.lang.System.arraycopy(this.k, 0, bArr, 4, 12);
        this.a.init(true, keyParameter);
        this.a.c(bArr, 0, bArr2, 0);
        java.lang.System.arraycopy(bArr2, 0, bArr3, 0, 8);
        bArr[0] = (byte) (bArr[0] + 1);
        this.a.c(bArr, 0, bArr2, 0);
        java.lang.System.arraycopy(bArr2, 0, bArr3, 8, 8);
        bArr[0] = (byte) (bArr[0] + 1);
        this.a.c(bArr, 0, bArr2, 0);
        java.lang.System.arraycopy(bArr2, 0, bArr4, 0, 8);
        bArr[0] = (byte) (bArr[0] + 1);
        this.a.c(bArr, 0, bArr2, 0);
        java.lang.System.arraycopy(bArr2, 0, bArr4, 8, 8);
        if (iC == 32) {
            bArr[0] = (byte) (bArr[0] + 1);
            this.a.c(bArr, 0, bArr2, 0);
            java.lang.System.arraycopy(bArr2, 0, bArr4, 16, 8);
            bArr[0] = (byte) (bArr[0] + 1);
            this.a.c(bArr, 0, bArr2, 0);
            java.lang.System.arraycopy(bArr2, 0, bArr4, 24, 8);
        }
        this.a.init(true, new org.bouncycastle.crypto.params.KeyParameter(bArr4));
        p(bArr3, 0, 16, bArr2);
        t(bArr2);
        this.b.a(bArr2);
        this.l |= 1;
    }

    private int o(byte[] bArr, byte[] bArr2, int i) {
        byte[] bArrD = this.g.d();
        byte[] bArrJ = org.bouncycastle.util.Arrays.j(bArr);
        bArrJ[15] = (byte) (bArrJ[15] | (-128));
        byte[] bArr3 = new byte[16];
        int size = this.g.size();
        int i2 = 0;
        while (size > 0) {
            this.a.c(bArrJ, 0, bArr3, 0);
            int iMin = java.lang.Math.min(16, size);
            w(bArr3, bArrD, i2, iMin);
            java.lang.System.arraycopy(bArr3, 0, bArr2, i + i2, iMin);
            size -= iMin;
            i2 += iMin;
            s(bArrJ);
        }
        return this.g.size();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void p(byte[] bArr, int i, int i2, byte[] bArr2) {
        int i3 = 0;
        int i4 = 15;
        while (i3 < i2) {
            bArr2[i4] = bArr[i + i3];
            i3++;
            i4--;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void q(byte[] bArr) {
        v(this.c, bArr);
        this.b.b(this.c);
    }

    private void r() {
        byte[] bArr = new byte[16];
        org.bouncycastle.util.Pack.C(this.f.b() * 8, bArr, 0);
        org.bouncycastle.util.Pack.C(this.e.b() * 8, bArr, 8);
        q(bArr);
    }

    private static void s(byte[] bArr) {
        for (int i = 0; i < 4; i++) {
            byte b = (byte) (bArr[i] + 1);
            bArr[i] = b;
            if (b != 0) {
                return;
            }
        }
    }

    private static void t(byte[] bArr) {
        int i = 0;
        for (int i2 = 0; i2 < 16; i2++) {
            byte b = bArr[i2];
            bArr[i2] = (byte) (i | ((b >> 1) & 127));
            i = (b & 1) == 0 ? 0 : -128;
        }
        if (i != 0) {
            bArr[0] = (byte) (bArr[0] ^ (-31));
        }
    }

    private void u() {
        org.bouncycastle.crypto.modes.GCMSIVBlockCipher.GCMSIVCache gCMSIVCache = this.g;
        if (gCMSIVCache != null) {
            gCMSIVCache.a();
        }
        this.e.c();
        this.f.c();
        this.g = new org.bouncycastle.crypto.modes.GCMSIVBlockCipher.GCMSIVCache();
        this.h = this.i ? null : new org.bouncycastle.crypto.modes.GCMSIVBlockCipher.GCMSIVCache();
        this.l &= -3;
        org.bouncycastle.util.Arrays.G(this.c, (byte) 0);
        byte[] bArr = this.j;
        if (bArr != null) {
            this.e.e(bArr, 0, bArr.length);
        }
    }

    private static void v(byte[] bArr, byte[] bArr2) {
        for (int i = 0; i < 16; i++) {
            bArr[i] = (byte) (bArr[i] ^ bArr2[i]);
        }
    }

    private static void w(byte[] bArr, byte[] bArr2, int i, int i2) {
        for (int i3 = 0; i3 < i2; i3++) {
            bArr[i3] = (byte) (bArr[i3] ^ bArr2[i3 + i]);
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public byte[] b() {
        return org.bouncycastle.util.Arrays.j(this.m);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void c(byte[] bArr, int i, int i2) {
        i(i2);
        j(bArr, i, i2, false);
        this.e.e(bArr, i, i2);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void d(byte b) {
        i(1);
        this.e.d(b);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int doFinal(byte[] bArr, int i) throws org.bouncycastle.crypto.InvalidCipherTextException, java.io.IOException {
        k(0);
        j(bArr, i, getOutputSize(0), true);
        if (!this.i) {
            m();
            int size = this.g.size();
            java.lang.System.arraycopy(this.g.d(), 0, bArr, i, size);
            u();
            return size;
        }
        byte[] bArrH = h();
        int iO = o(bArrH, bArr, i) + 16;
        java.lang.System.arraycopy(bArrH, 0, bArr, i + this.g.size(), 16);
        byte[] bArr2 = this.m;
        java.lang.System.arraycopy(bArrH, 0, bArr2, 0, bArr2.length);
        u();
        return iO;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public java.lang.String getAlgorithmName() {
        return this.a.getAlgorithmName() + "-GCM-SIV";
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getOutputSize(int i) {
        if (this.i) {
            return i + this.g.size() + 16;
        }
        int size = i + this.h.size();
        if (size > 16) {
            return size - 16;
        }
        return 0;
    }

    @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) {
        return 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;
        byte[] bArrA2;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.AEADParameters) {
            org.bouncycastle.crypto.params.AEADParameters aEADParameters = (org.bouncycastle.crypto.params.AEADParameters) cipherParameters;
            bArrA2 = aEADParameters.a();
            bArrA = aEADParameters.d();
            keyParameterB = aEADParameters.b();
        } else {
            if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
                throw new java.lang.IllegalArgumentException("invalid parameters passed to GCM-SIV");
            }
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            bArrA = parametersWithIV.a();
            keyParameterB = (org.bouncycastle.crypto.params.KeyParameter) parametersWithIV.b();
            bArrA2 = null;
        }
        if (bArrA == null || bArrA.length != 12) {
            throw new java.lang.IllegalArgumentException("Invalid nonce");
        }
        if (keyParameterB == null || !(keyParameterB.c() == 16 || keyParameterB.c() == 32)) {
            throw new java.lang.IllegalArgumentException("Invalid key");
        }
        this.i = z;
        this.j = bArrA2;
        this.k = bArrA;
        n(keyParameterB);
        u();
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processByte(byte b, byte[] bArr, int i) throws java.io.IOException {
        k(1);
        if (!this.i) {
            this.h.write(b);
            return 0;
        }
        this.g.write(b);
        this.f.d(b);
        return 0;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws java.io.IOException {
        k(i2);
        j(bArr, i, i2, false);
        if (this.i) {
            this.g.write(bArr, i, i2);
            this.f.e(bArr, i, i2);
        } else {
            this.h.write(bArr, i, i2);
        }
        return 0;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void reset() {
        u();
    }

    public GCMSIVBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher, org.bouncycastle.crypto.modes.gcm.GCMMultiplier gCMMultiplier) {
        this.c = new byte[16];
        this.d = new byte[16];
        this.m = new byte[16];
        if (blockCipher.b() != 16) {
            throw new java.lang.IllegalArgumentException("Cipher required with a block size of 16.");
        }
        this.a = blockCipher;
        this.b = gCMMultiplier;
        this.e = new org.bouncycastle.crypto.modes.GCMSIVBlockCipher.GCMSIVHasher();
        this.f = new org.bouncycastle.crypto.modes.GCMSIVBlockCipher.GCMSIVHasher();
    }
}
