package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
abstract class AEADBufferBaseEngine extends org.bouncycastle.crypto.engines.AEADBaseEngine {
    protected byte[] h;
    protected byte[] i;
    protected int j;
    protected int k;
    protected boolean l;
    protected boolean m;
    protected int n;
    protected int o;
    protected org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State p;

    /* JADX INFO: renamed from: org.bouncycastle.crypto.engines.AEADBufferBaseEngine$1, reason: invalid class name */
    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.values().length];
            a = iArr;
            try {
                iArr[org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.DecInit.ordinal()] = 1;
            } catch (java.lang.NoSuchFieldError unused) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.DecAad.ordinal()] = 2;
            } catch (java.lang.NoSuchFieldError unused2) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.DecData.ordinal()] = 3;
            } catch (java.lang.NoSuchFieldError unused3) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.DecFinal.ordinal()] = 4;
            } catch (java.lang.NoSuchFieldError unused4) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.EncData.ordinal()] = 5;
            } catch (java.lang.NoSuchFieldError unused5) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.EncFinal.ordinal()] = 6;
            } catch (java.lang.NoSuchFieldError unused6) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.EncInit.ordinal()] = 7;
            } catch (java.lang.NoSuchFieldError unused7) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.EncAad.ordinal()] = 8;
            } catch (java.lang.NoSuchFieldError unused8) {
            }
        }
    }

    protected enum State {
        Uninitialized,
        EncInit,
        EncAad,
        EncData,
        EncFinal,
        DecInit,
        DecAad,
        DecData,
        DecFinal
    }

    private void i(org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State state) {
        int i = org.bouncycastle.crypto.engines.AEADBufferBaseEngine.AnonymousClass1.a[this.p.ordinal()];
        if (i == 2 || i == 8) {
            l();
        }
        this.k = 0;
        this.p = state;
    }

    /* JADX WARN: Code duplicated, block: B:16:0x0032 A[LOOP:0: B:14:0x002e->B:16:0x0032, LOOP_END] */
    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void c(byte[] bArr, int i, int i2) {
        if (i + i2 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        if (i2 <= 0) {
            return;
        }
        g();
        int i3 = this.k;
        if (i3 <= 0) {
            while (i2 > this.n) {
                k(bArr, i);
                int i4 = this.n;
                i += i4;
                i2 -= i4;
            }
            java.lang.System.arraycopy(bArr, i, this.i, this.k, i2);
        } else {
            int i5 = this.n - i3;
            if (i2 <= i5) {
                java.lang.System.arraycopy(bArr, i, this.i, i3, i2);
            } else {
                java.lang.System.arraycopy(bArr, i, this.i, i3, i5);
                i += i5;
                i2 -= i5;
                k(this.i, 0);
                this.k = 0;
                while (i2 > this.n) {
                    k(bArr, i);
                    int i6 = this.n;
                    i += i6;
                    i2 -= i6;
                }
                java.lang.System.arraycopy(bArr, i, this.i, this.k, i2);
            }
        }
        this.k += i2;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int doFinal(byte[] bArr, int i) throws org.bouncycastle.crypto.InvalidCipherTextException {
        int i2;
        boolean zH = h();
        int i3 = this.j;
        int i4 = this.e;
        if (zH) {
            i2 = i3 + i4;
        } else {
            if (i3 < i4) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("data too short");
            }
            i2 = i3 - i4;
            this.j = i2;
        }
        if (i > bArr.length - i2) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        m(bArr, i);
        if (zH) {
            byte[] bArr2 = this.g;
            int i5 = this.e;
            java.lang.System.arraycopy(bArr2, 0, bArr, (i + i2) - i5, i5);
        } else if (!org.bouncycastle.util.Arrays.y(this.e, this.g, 0, this.h, this.j)) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException(this.b + " mac does not match");
        }
        e(!zH);
        return i2;
    }

    protected void f() {
        org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State state;
        org.bouncycastle.util.Arrays.G(this.h, (byte) 0);
        org.bouncycastle.util.Arrays.G(this.i, (byte) 0);
        this.j = 0;
        this.k = 0;
        switch (org.bouncycastle.crypto.engines.AEADBufferBaseEngine.AnonymousClass1.a[this.p.ordinal()]) {
            case 1:
            case 7:
                return;
            case 2:
            case 3:
            case 4:
                state = org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.DecInit;
                break;
            case 5:
            case 6:
            case 8:
                state = org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.EncFinal;
                break;
            default:
                throw new java.lang.IllegalStateException(getAlgorithmName() + " needs to be initialized");
        }
        this.p = state;
    }

    protected void g() {
        org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State state;
        int i = org.bouncycastle.crypto.engines.AEADBufferBaseEngine.AnonymousClass1.a[this.p.ordinal()];
        if (i == 1) {
            state = org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.DecAad;
        } else {
            if (i == 2) {
                return;
            }
            if (i == 6) {
                throw new java.lang.IllegalStateException(getAlgorithmName() + " cannot be reused for encryption");
            }
            if (i != 7) {
                if (i == 8) {
                    return;
                }
                throw new java.lang.IllegalStateException(getAlgorithmName() + " needs to be initialized");
            }
            state = org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.EncAad;
        }
        this.p = state;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getOutputSize(int i) {
        int iMax = java.lang.Math.max(0, i);
        switch (org.bouncycastle.crypto.engines.AEADBufferBaseEngine.AnonymousClass1.a[this.p.ordinal()]) {
            case 1:
            case 2:
                break;
            case 3:
            case 4:
                iMax += this.j;
                break;
            case 5:
            case 6:
                iMax += this.j;
            default:
                return iMax + this.e;
        }
        return java.lang.Math.max(0, iMax - this.e);
    }

    /* JADX WARN: Failed to find 'out' block for switch in B:3:0x0011. Please report as an issue. */
    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getUpdateOutputSize(int i) {
        int i2;
        int iMax = java.lang.Math.max(0, i) - 1;
        switch (org.bouncycastle.crypto.engines.AEADBufferBaseEngine.AnonymousClass1.a[this.p.ordinal()]) {
            case 3:
            case 4:
                iMax += this.j;
            case 1:
            case 2:
                i2 = iMax - this.e;
                iMax = java.lang.Math.max(0, i2);
                break;
            case 5:
            case 6:
                i2 = iMax + this.j;
                iMax = java.lang.Math.max(0, i2);
                break;
        }
        return iMax - (iMax % this.o);
    }

    protected boolean h() {
        switch (org.bouncycastle.crypto.engines.AEADBufferBaseEngine.AnonymousClass1.a[this.p.ordinal()]) {
            case 1:
            case 2:
                i(org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.DecData);
                return false;
            case 3:
                return false;
            case 4:
            default:
                throw new java.lang.IllegalStateException(getAlgorithmName() + " needs to be initialized");
            case 5:
                return true;
            case 6:
                throw new java.lang.IllegalStateException(getAlgorithmName() + " cannot be reused for encryption");
            case 7:
            case 8:
                i(org.bouncycastle.crypto.engines.AEADBufferBaseEngine.State.EncData);
                return true;
        }
    }

    protected abstract void j(byte[] bArr, int i, byte[] bArr2, int i2);

    protected abstract void k(byte[] bArr, int i);

    protected abstract void l();

    protected abstract void m(byte[] bArr, int i);

    protected void n(byte[] bArr, int i, byte[] bArr2, int i2) {
        if (i2 > bArr2.length - this.o) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        j(bArr, i, bArr2, i2);
    }

    /* JADX WARN: Code duplicated, block: B:44:0x00ea  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:44:0x00ea -> B:29:0x008d). Please report as a decompilation issue!!! */
    /*  JADX ERROR: JadxOverflowException in pass: RegionMakerVisitor
        jadx.core.utils.exceptions.JadxOverflowException: Regions stack size limit reached
        	at jadx.core.utils.ErrorsCounter.addError(ErrorsCounter.java:59)
        	at jadx.core.utils.ErrorsCounter.error(ErrorsCounter.java:31)
        	at jadx.core.dex.attributes.nodes.NotificationAttrNode.addError(NotificationAttrNode.java:19)
        */
    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processBytes(byte[] r7, int r8, int r9, byte[] r10, int r11) {
        /*
            Method dump skipped, instruction units count: 260
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: org.bouncycastle.crypto.engines.AEADBufferBaseEngine.processBytes(byte[], int, int, byte[], int):int");
    }
}
