package org.bouncycastle.crypto.engines;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
abstract class AsconBaseEngine extends org.bouncycastle.crypto.engines.AEADBaseEngine {
    protected org.bouncycastle.crypto.engines.AsconBaseEngine.State h;
    protected int i;
    protected int j;
    protected long k;
    protected long l;
    protected long m;
    protected long n;
    protected long o;
    protected long p;
    protected long q;
    protected long r;
    protected long s;
    protected long t;
    protected int u;
    protected byte[] v;
    protected int w;
    protected long x;

    /* JADX INFO: renamed from: org.bouncycastle.crypto.engines.AsconBaseEngine$1, reason: invalid class name */
    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[org.bouncycastle.crypto.engines.AsconBaseEngine.State.values().length];
            a = iArr;
            try {
                iArr[org.bouncycastle.crypto.engines.AsconBaseEngine.State.DecInit.ordinal()] = 1;
            } catch (java.lang.NoSuchFieldError unused) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AsconBaseEngine.State.EncInit.ordinal()] = 2;
            } catch (java.lang.NoSuchFieldError unused2) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AsconBaseEngine.State.DecAad.ordinal()] = 3;
            } catch (java.lang.NoSuchFieldError unused3) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AsconBaseEngine.State.EncAad.ordinal()] = 4;
            } catch (java.lang.NoSuchFieldError unused4) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AsconBaseEngine.State.EncFinal.ordinal()] = 5;
            } catch (java.lang.NoSuchFieldError unused5) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AsconBaseEngine.State.DecData.ordinal()] = 6;
            } catch (java.lang.NoSuchFieldError unused6) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AsconBaseEngine.State.EncData.ordinal()] = 7;
            } catch (java.lang.NoSuchFieldError unused7) {
            }
            try {
                a[org.bouncycastle.crypto.engines.AsconBaseEngine.State.DecFinal.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.AsconBaseEngine.State state) {
        int i = org.bouncycastle.crypto.engines.AsconBaseEngine.AnonymousClass1.a[this.h.ordinal()];
        if (i == 3 || i == 4) {
            o();
            k(this.i);
        }
        this.t ^= this.x;
        this.w = 0;
        this.h = state;
    }

    private void r(long j) {
        long j2 = this.p;
        long j3 = this.q;
        long j4 = this.r;
        long j5 = this.s;
        long j6 = this.t;
        long j7 = ((((j2 ^ j3) ^ j4) ^ j5) ^ j) ^ ((((j2 ^ j4) ^ j6) ^ j) & j3);
        long j8 = ((((j2 ^ j4) ^ j5) ^ j6) ^ j) ^ (((j3 ^ j4) ^ j) & (j3 ^ j5));
        long j9 = (((j3 ^ j4) ^ j6) ^ j) ^ (j5 & j6);
        long j10 = ((j4 ^ (j2 ^ j3)) ^ j) ^ ((~j2) & (j5 ^ j6));
        long j11 = ((j2 ^ j6) & j3) ^ ((j3 ^ j5) ^ j6);
        this.p = org.bouncycastle.util.Longs.c(j7, 28) ^ (org.bouncycastle.util.Longs.c(j7, 19) ^ j7);
        this.q = (org.bouncycastle.util.Longs.c(j8, 39) ^ j8) ^ org.bouncycastle.util.Longs.c(j8, 61);
        this.r = ~(org.bouncycastle.util.Longs.c(j9, 6) ^ (org.bouncycastle.util.Longs.c(j9, 1) ^ j9));
        this.s = (org.bouncycastle.util.Longs.c(j10, 10) ^ j10) ^ org.bouncycastle.util.Longs.c(j10, 17);
        this.t = org.bouncycastle.util.Longs.c(j11, 41) ^ (org.bouncycastle.util.Longs.c(j11, 7) ^ j11);
    }

    @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.w;
        if (i3 > 0) {
            int i4 = this.j - i3;
            if (i2 < i4) {
                java.lang.System.arraycopy(bArr, i, this.v, i3, i2);
                this.w += i2;
                return;
            } else {
                java.lang.System.arraycopy(bArr, i, this.v, i3, i4);
                i += i4;
                i2 -= i4;
                l(this.v, 0);
            }
        }
        while (i2 >= this.j) {
            l(bArr, i);
            int i5 = this.j;
            i += i5;
            i2 -= i5;
        }
        java.lang.System.arraycopy(bArr, i, this.v, 0, i2);
        this.w = i2;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int doFinal(byte[] bArr, int i) throws org.bouncycastle.crypto.InvalidCipherTextException {
        if (h()) {
            int i2 = this.w;
            int i3 = this.e + i2;
            if (i + i3 > bArr.length) {
                throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
            }
            q(this.v, i2, bArr, i);
            byte[] bArr2 = new byte[this.e];
            this.g = bArr2;
            s(this.s, bArr2, 0);
            s(this.t, this.g, 8);
            java.lang.System.arraycopy(this.g, 0, bArr, i + this.w, this.e);
            e(false);
            return i3;
        }
        int i4 = this.w;
        int i5 = this.e;
        if (i4 < i5) {
            throw new org.bouncycastle.crypto.InvalidCipherTextException("data too short");
        }
        int i6 = i4 - i5;
        this.w = i6;
        if (i + i6 > bArr.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        p(this.v, i6, bArr, i);
        this.s ^= j(this.v, this.w);
        long j = this.t ^ j(this.v, this.w + 8);
        this.t = j;
        if ((j | this.s) == 0) {
            e(true);
            return i6;
        }
        throw new org.bouncycastle.crypto.InvalidCipherTextException("mac check in " + getAlgorithmName() + " failed");
    }

    @Override // org.bouncycastle.crypto.engines.AEADBaseEngine
    protected void e(boolean z) {
        org.bouncycastle.util.Arrays.i(this.v);
        this.w = 0;
        switch (org.bouncycastle.crypto.engines.AsconBaseEngine.AnonymousClass1.a[this.h.ordinal()]) {
            case 1:
            case 2:
                break;
            case 3:
            case 6:
            case 8:
                this.h = org.bouncycastle.crypto.engines.AsconBaseEngine.State.DecInit;
                break;
            case 4:
            case 5:
            case 7:
                this.h = org.bouncycastle.crypto.engines.AsconBaseEngine.State.EncFinal;
                return;
            default:
                throw new java.lang.IllegalStateException(getAlgorithmName() + " needs to be initialized");
        }
        f();
        super.e(z);
    }

    protected abstract void f();

    protected void g() {
        org.bouncycastle.crypto.engines.AsconBaseEngine.State state;
        int i = org.bouncycastle.crypto.engines.AsconBaseEngine.AnonymousClass1.a[this.h.ordinal()];
        if (i == 1) {
            state = org.bouncycastle.crypto.engines.AsconBaseEngine.State.DecAad;
        } else {
            if (i != 2) {
                if (i == 3 || i == 4) {
                    return;
                }
                if (i != 5) {
                    throw new java.lang.IllegalStateException(getAlgorithmName() + " needs to be initialized");
                }
                throw new java.lang.IllegalStateException(getAlgorithmName() + " cannot be reused for encryption");
            }
            state = org.bouncycastle.crypto.engines.AsconBaseEngine.State.EncAad;
        }
        this.h = state;
    }

    /* JADX WARN: Code duplicated, block: B:19:0x0031  */
    /* JADX WARN: Code restructure failed: missing block: B:13:0x0020, code lost:
    
        if (r1 != 8) goto L14;
     */
    @Override // org.bouncycastle.crypto.modes.AEADCipher
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public int getOutputSize(int r4) {
        /*
            r3 = this;
            r0 = 0
            int r4 = java.lang.Math.max(r0, r4)
            int[] r1 = org.bouncycastle.crypto.engines.AsconBaseEngine.AnonymousClass1.a
            org.bouncycastle.crypto.engines.AsconBaseEngine$State r2 = r3.h
            int r2 = r2.ordinal()
            r1 = r1[r2]
            r2 = 1
            if (r1 == r2) goto L29
            r2 = 3
            if (r1 == r2) goto L29
            r2 = 5
            if (r1 == r2) goto L31
            r2 = 6
            if (r1 == r2) goto L26
            r2 = 7
            if (r1 == r2) goto L31
            r2 = 8
            if (r1 == r2) goto L26
        L22:
            int r3 = r3.e
            int r4 = r4 + r3
            return r4
        L26:
            int r1 = r3.w
            int r4 = r4 + r1
        L29:
            int r3 = r3.e
            int r4 = r4 - r3
            int r3 = java.lang.Math.max(r0, r4)
            return r3
        L31:
            int r0 = r3.w
            int r4 = r4 + r0
            goto L22
        */
        throw new UnsupportedOperationException("Method not decompiled: org.bouncycastle.crypto.engines.AsconBaseEngine.getOutputSize(int):int");
    }

    /* JADX WARN: Code duplicated, block: B:17:0x002e  */
    /* JADX WARN: Code restructure failed: missing block: B:13:0x0020, code lost:
    
        if (r1 != 8) goto L18;
     */
    @Override // org.bouncycastle.crypto.modes.AEADCipher
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public int getUpdateOutputSize(int r4) {
        /*
            r3 = this;
            r0 = 0
            int r4 = java.lang.Math.max(r0, r4)
            int[] r1 = org.bouncycastle.crypto.engines.AsconBaseEngine.AnonymousClass1.a
            org.bouncycastle.crypto.engines.AsconBaseEngine$State r2 = r3.h
            int r2 = r2.ordinal()
            r1 = r1[r2]
            r2 = 1
            if (r1 == r2) goto L26
            r2 = 3
            if (r1 == r2) goto L26
            r2 = 5
            if (r1 == r2) goto L2e
            r2 = 6
            if (r1 == r2) goto L23
            r2 = 7
            if (r1 == r2) goto L2e
            r2 = 8
            if (r1 == r2) goto L23
            goto L31
        L23:
            int r1 = r3.w
            int r4 = r4 + r1
        L26:
            int r1 = r3.e
            int r4 = r4 - r1
            int r4 = java.lang.Math.max(r0, r4)
            goto L31
        L2e:
            int r0 = r3.w
            int r4 = r4 + r0
        L31:
            int r3 = r3.j
            int r3 = r4 % r3
            int r4 = r4 - r3
            return r4
        */
        throw new UnsupportedOperationException("Method not decompiled: org.bouncycastle.crypto.engines.AsconBaseEngine.getUpdateOutputSize(int):int");
    }

    protected boolean h() {
        switch (org.bouncycastle.crypto.engines.AsconBaseEngine.AnonymousClass1.a[this.h.ordinal()]) {
            case 1:
            case 3:
                i(org.bouncycastle.crypto.engines.AsconBaseEngine.State.DecData);
                return false;
            case 2:
            case 4:
                i(org.bouncycastle.crypto.engines.AsconBaseEngine.State.EncData);
                return true;
            case 5:
                throw new java.lang.IllegalStateException(getAlgorithmName() + " cannot be reused for encryption");
            case 6:
                return false;
            case 7:
                return true;
            default:
                throw new java.lang.IllegalStateException(getAlgorithmName() + " needs to be initialized");
        }
    }

    protected abstract long j(byte[] bArr, int i);

    protected void k(int i) {
        if (i == 12) {
            r(240L);
            r(225L);
            r(210L);
            r(195L);
        }
        if (i >= 8) {
            r(180L);
            r(165L);
        }
        r(150L);
        r(135L);
        r(120L);
        r(105L);
        r(90L);
        r(75L);
    }

    protected void l(byte[] bArr, int i) {
        this.p ^= j(bArr, i);
        if (this.j == 16) {
            this.q = j(bArr, i + 8) ^ this.q;
        }
        k(this.i);
    }

    protected void m(byte[] bArr, int i, byte[] bArr2, int i2) {
        if (this.j + i2 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        long j = j(bArr, i);
        s(this.p ^ j, bArr2, i2);
        this.p = j;
        if (this.j == 16) {
            long j2 = j(bArr, i + 8);
            s(this.q ^ j2, bArr2, i2 + 8);
            this.q = j2;
        }
        k(this.i);
    }

    protected void n(byte[] bArr, int i, byte[] bArr2, int i2) {
        if (this.j + i2 > bArr2.length) {
            throw new org.bouncycastle.crypto.OutputLengthException("output buffer too short");
        }
        long j = this.p ^ j(bArr, i);
        this.p = j;
        s(j, bArr2, i2);
        if (this.j == 16) {
            long j2 = j(bArr, i + 8) ^ this.q;
            this.q = j2;
            s(j2, bArr2, i2 + 8);
        }
        k(this.i);
    }

    protected abstract void o();

    protected abstract void p(byte[] bArr, int i, byte[] bArr2, int i2);

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        int i4;
        if (i + i2 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        if (h()) {
            int i5 = this.w;
            if (i5 > 0) {
                int i6 = this.j - i5;
                if (i2 < i6) {
                    java.lang.System.arraycopy(bArr, i, this.v, i5, i2);
                    this.w += i2;
                    return 0;
                }
                java.lang.System.arraycopy(bArr, i, this.v, i5, i6);
                i += i6;
                i2 -= i6;
                n(this.v, 0, bArr2, i3);
                i4 = this.j;
            } else {
                i4 = 0;
            }
            while (i2 >= this.j) {
                n(bArr, i, bArr2, i3 + i4);
                int i7 = this.j;
                i += i7;
                i2 -= i7;
                i4 += i7;
            }
            java.lang.System.arraycopy(bArr, i, this.v, 0, i2);
            this.w = i2;
            return i4;
        }
        int i8 = this.u;
        int i9 = this.w;
        int i10 = i8 - i9;
        if (i2 < i10) {
            java.lang.System.arraycopy(bArr, i, this.v, i9, i2);
            this.w += i2;
            return 0;
        }
        int i11 = 0;
        do {
            int i12 = this.w;
            int i13 = this.j;
            if (i12 < i13) {
                int i14 = i13 - i12;
                java.lang.System.arraycopy(bArr, i, this.v, i12, i14);
                i += i14;
                i2 -= i14;
                m(this.v, 0, bArr2, i3 + i11);
                i4 = i11 + this.j;
                while (i2 >= this.u) {
                    m(bArr, i, bArr2, i3 + i4);
                    int i15 = this.j;
                    i += i15;
                    i2 -= i15;
                    i4 += i15;
                }
                java.lang.System.arraycopy(bArr, i, this.v, 0, i2);
                this.w = i2;
                return i4;
            }
            m(this.v, 0, bArr2, i3 + i11);
            int i16 = this.w;
            int i17 = this.j;
            int i18 = i16 - i17;
            this.w = i18;
            byte[] bArr3 = this.v;
            java.lang.System.arraycopy(bArr3, i17, bArr3, 0, i18);
            int i19 = this.j;
            i11 += i19;
            i10 += i19;
        } while (i2 >= i10);
        java.lang.System.arraycopy(bArr, i, this.v, this.w, i2);
        this.w += i2;
        return i11;
    }

    protected abstract void q(byte[] bArr, int i, byte[] bArr2, int i2);

    protected abstract void s(long j, byte[] bArr, int i);
}
