package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ChaCha20Poly1305 implements org.bouncycastle.crypto.modes.AEADCipher {
    private static final byte[] l = new byte[15];
    private final org.bouncycastle.crypto.engines.ChaCha7539Engine a;
    private final org.bouncycastle.crypto.Mac b;
    private final byte[] c;
    private final byte[] d;
    private final byte[] e;
    private final byte[] f;
    private byte[] g;
    private long h;
    private long i;
    private int j;
    private int k;

    private static final class State {
    }

    public ChaCha20Poly1305() {
        this(new org.bouncycastle.crypto.macs.Poly1305());
    }

    private void a() {
        int i = this.j;
        if (i == 1) {
            this.j = 2;
            return;
        }
        if (i != 2) {
            if (i == 4) {
                throw new java.lang.IllegalStateException("ChaCha20Poly1305 cannot be reused for encryption");
            }
            if (i == 5) {
                this.j = 6;
            } else if (i != 6) {
                throw new java.lang.IllegalStateException();
            }
        }
    }

    private void e() {
        int i;
        switch (this.j) {
            case 1:
            case 2:
                i = 3;
                break;
            case 3:
            case 7:
                return;
            case 4:
                throw new java.lang.IllegalStateException("ChaCha20Poly1305 cannot be reused for encryption");
            case 5:
            case 6:
                i = 7;
                break;
            default:
                throw new java.lang.IllegalStateException();
        }
        f(i);
    }

    private void f(int i) {
        j(this.h);
        this.j = i;
    }

    private void g(int i) {
        j(this.i);
        byte[] bArr = new byte[16];
        org.bouncycastle.util.Pack.H(this.h, bArr, 0);
        org.bouncycastle.util.Pack.H(this.i, bArr, 8);
        this.b.update(bArr, 0, 16);
        this.b.doFinal(this.f, 0);
        this.j = i;
    }

    private long h(long j, int i, long j2) {
        long j3 = i;
        if (j - Long.MIN_VALUE <= (j2 - j3) - Long.MIN_VALUE) {
            return j + j3;
        }
        throw new java.lang.IllegalStateException("Limit exceeded");
    }

    private void i() {
        byte[] bArr = new byte[64];
        try {
            this.a.processBytes(bArr, 0, 64, bArr, 0);
            this.b.init(new org.bouncycastle.crypto.params.KeyParameter(bArr, 0, 32));
        } finally {
            org.bouncycastle.util.Arrays.i(bArr);
        }
    }

    private void j(long j) {
        int i = ((int) j) & 15;
        if (i != 0) {
            this.b.update(l, 0, 16 - i);
        }
    }

    private void k(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        if (i3 > bArr2.length - i2) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
        }
        this.a.processBytes(bArr, i, i2, bArr2, i3);
        this.i = h(this.i, i2, 274877906880L);
    }

    private void l(boolean z, boolean z2) {
        org.bouncycastle.util.Arrays.i(this.e);
        if (z) {
            org.bouncycastle.util.Arrays.i(this.f);
        }
        this.h = 0L;
        this.i = 0L;
        this.k = 0;
        switch (this.j) {
            case 1:
            case 5:
                break;
            case 2:
            case 3:
            case 4:
                this.j = 4;
                return;
            case 6:
            case 7:
            case 8:
                this.j = 5;
                break;
            default:
                throw new java.lang.IllegalStateException();
        }
        if (z2) {
            this.a.reset();
        }
        i();
        byte[] bArr = this.g;
        if (bArr != null) {
            c(bArr, 0, bArr.length);
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public byte[] b() {
        return org.bouncycastle.util.Arrays.j(this.f);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void c(byte[] bArr, int i, int i2) {
        if (bArr == null) {
            throw new java.lang.NullPointerException("'in' cannot be null");
        }
        if (i < 0) {
            throw new java.lang.IllegalArgumentException("'inOff' cannot be negative");
        }
        if (i2 < 0) {
            throw new java.lang.IllegalArgumentException("'len' cannot be negative");
        }
        if (i > bArr.length - i2) {
            throw new org.bouncycastle.crypto.DataLengthException("Input buffer too short");
        }
        a();
        if (i2 > 0) {
            this.h = h(this.h, i2, -1L);
            this.b.update(bArr, i, i2);
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int doFinal(byte[] bArr, int i) throws org.bouncycastle.crypto.InvalidCipherTextException {
        int i2;
        int i3;
        if (bArr == null) {
            throw new java.lang.NullPointerException("'out' cannot be null");
        }
        if (i < 0) {
            throw new java.lang.IllegalArgumentException("'outOff' cannot be negative");
        }
        e();
        org.bouncycastle.util.Arrays.i(this.f);
        int i4 = this.j;
        if (i4 == 3) {
            int i5 = this.k;
            int i6 = i5 + 16;
            if (i > bArr.length - i6) {
                throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
            }
            if (i5 > 0) {
                k(this.e, 0, i5, bArr, i);
                this.b.update(bArr, i, this.k);
            }
            g(4);
            java.lang.System.arraycopy(this.f, 0, bArr, this.k + i, 16);
            i2 = i6;
        } else {
            if (i4 != 7) {
                throw new java.lang.IllegalStateException();
            }
            int i7 = this.k;
            if (i7 < 16) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("data too short");
            }
            int i8 = i7 - 16;
            if (i > bArr.length - i8) {
                throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short");
            }
            if (i8 > 0) {
                this.b.update(this.e, 0, i8);
                i3 = i8;
                k(this.e, 0, i3, bArr, i);
            } else {
                i3 = i8;
            }
            g(8);
            if (!org.bouncycastle.util.Arrays.y(16, this.f, 0, this.e, i3)) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("mac check in ChaCha20Poly1305 failed");
            }
            i2 = i3;
        }
        l(false, true);
        return i2;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public java.lang.String getAlgorithmName() {
        return "ChaCha20Poly1305";
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getOutputSize(int i) {
        int iMax = java.lang.Math.max(0, i) + this.k;
        int i2 = this.j;
        if (i2 == 1 || i2 == 2 || i2 == 3) {
            return iMax + 16;
        }
        if (i2 == 5 || i2 == 6 || i2 == 7) {
            return java.lang.Math.max(0, iMax - 16);
        }
        throw new java.lang.IllegalStateException();
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getUpdateOutputSize(int i) {
        int iMax = java.lang.Math.max(0, i) + this.k;
        int i2 = this.j;
        if (i2 != 1 && i2 != 2 && i2 != 3) {
            if (i2 != 5 && i2 != 6 && i2 != 7) {
                throw new java.lang.IllegalStateException();
            }
            iMax = java.lang.Math.max(0, iMax - 16);
        }
        return iMax - (iMax % 64);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV;
        org.bouncycastle.crypto.params.KeyParameter keyParameterB;
        byte[] bArrA;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.AEADParameters) {
            org.bouncycastle.crypto.params.AEADParameters aEADParameters = (org.bouncycastle.crypto.params.AEADParameters) cipherParameters;
            int iC = aEADParameters.c();
            if (128 != iC) {
                throw new java.lang.IllegalArgumentException("Invalid value for MAC size: " + iC);
            }
            keyParameterB = aEADParameters.b();
            bArrA = aEADParameters.d();
            parametersWithIV = new org.bouncycastle.crypto.params.ParametersWithIV(keyParameterB, bArrA);
            this.g = aEADParameters.a();
        } else {
            if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
                throw new java.lang.IllegalArgumentException("invalid parameters passed to ChaCha20Poly1305");
            }
            parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            keyParameterB = (org.bouncycastle.crypto.params.KeyParameter) parametersWithIV.b();
            bArrA = parametersWithIV.a();
            this.g = null;
        }
        if (keyParameterB == null) {
            if (this.j == 0) {
                throw new java.lang.IllegalArgumentException("Key must be specified in initial init");
            }
        } else if (32 != keyParameterB.c()) {
            throw new java.lang.IllegalArgumentException("Key must be 256 bits");
        }
        if (bArrA == null || 12 != bArrA.length) {
            throw new java.lang.IllegalArgumentException("Nonce must be 96 bits");
        }
        if (this.j != 0 && z && org.bouncycastle.util.Arrays.d(this.d, bArrA) && (keyParameterB == null || org.bouncycastle.util.Arrays.d(this.c, keyParameterB.b()))) {
            throw new java.lang.IllegalArgumentException("cannot reuse nonce for ChaCha20Poly1305 encryption");
        }
        if (keyParameterB != null) {
            keyParameterB.a(this.c, 0, 32);
        }
        java.lang.System.arraycopy(bArrA, 0, this.d, 0, 12);
        this.a.init(true, parametersWithIV);
        this.j = z ? 1 : 5;
        l(true, false);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processByte(byte b, byte[] bArr, int i) {
        e();
        int i2 = this.j;
        if (i2 == 3) {
            byte[] bArr2 = this.e;
            int i3 = this.k;
            bArr2[i3] = b;
            int i4 = i3 + 1;
            this.k = i4;
            if (i4 != 64) {
                return 0;
            }
            k(bArr2, 0, 64, bArr, i);
            this.b.update(bArr, i, 64);
            this.k = 0;
            return 64;
        }
        if (i2 != 7) {
            throw new java.lang.IllegalStateException();
        }
        byte[] bArr3 = this.e;
        int i5 = this.k;
        bArr3[i5] = b;
        int i6 = i5 + 1;
        this.k = i6;
        if (i6 != bArr3.length) {
            return 0;
        }
        this.b.update(bArr3, 0, 64);
        k(this.e, 0, 64, bArr, i);
        byte[] bArr4 = this.e;
        java.lang.System.arraycopy(bArr4, 64, bArr4, 0, 16);
        this.k = 16;
        return 64;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processBytes(byte[] bArr, int i, int i2, byte[] bArr2, int i3) {
        int i4;
        int i5;
        if (bArr == null) {
            throw new java.lang.NullPointerException("'in' cannot be null");
        }
        if (i < 0) {
            throw new java.lang.IllegalArgumentException("'inOff' cannot be negative");
        }
        if (i2 < 0) {
            throw new java.lang.IllegalArgumentException("'len' cannot be negative");
        }
        if (i > bArr.length - i2) {
            throw new org.bouncycastle.crypto.DataLengthException("Input buffer too short");
        }
        if (i3 < 0) {
            throw new java.lang.IllegalArgumentException("'outOff' cannot be negative");
        }
        e();
        int i6 = this.j;
        if (i6 != 3) {
            if (i6 != 7) {
                throw new java.lang.IllegalStateException();
            }
            int i7 = 0;
            for (int i8 = 0; i8 < i2; i8++) {
                byte[] bArr3 = this.e;
                int i9 = this.k;
                bArr3[i9] = bArr[i + i8];
                int i10 = i9 + 1;
                this.k = i10;
                if (i10 == bArr3.length) {
                    this.b.update(bArr3, 0, 64);
                    k(this.e, 0, 64, bArr2, i3 + i7);
                    byte[] bArr4 = this.e;
                    java.lang.System.arraycopy(bArr4, 64, bArr4, 0, 16);
                    this.k = 16;
                    i7 += 64;
                }
            }
            return i7;
        }
        if (this.k == 0) {
            i4 = i;
            i5 = 0;
            break;
        }
        while (true) {
            if (i2 <= 0) {
                i4 = i;
                i5 = 0;
                break;
            }
            i2--;
            byte[] bArr5 = this.e;
            int i11 = this.k;
            int i12 = i + 1;
            bArr5[i11] = bArr[i];
            int i13 = i11 + 1;
            this.k = i13;
            if (i13 == 64) {
                k(bArr5, 0, 64, bArr2, i3);
                this.b.update(bArr2, i3, 64);
                this.k = 0;
                i5 = 64;
                i4 = i12;
                break;
            }
            i = i12;
        }
        while (i2 >= 64) {
            int i14 = i3 + i5;
            k(bArr, i4, 64, bArr2, i14);
            this.b.update(bArr2, i14, 64);
            i4 += 64;
            i2 -= 64;
            i5 += 64;
        }
        if (i2 > 0) {
            java.lang.System.arraycopy(bArr, i4, this.e, 0, i2);
            this.k = i2;
        }
        return i5;
    }

    public ChaCha20Poly1305(org.bouncycastle.crypto.Mac mac) {
        this.c = new byte[32];
        this.d = new byte[12];
        this.e = new byte[80];
        this.f = new byte[16];
        this.j = 0;
        if (mac == null) {
            throw new java.lang.NullPointerException("'poly1305' cannot be null");
        }
        if (16 != mac.getMacSize()) {
            throw new java.lang.IllegalArgumentException("'poly1305' must be a 128-bit MAC");
        }
        this.a = new org.bouncycastle.crypto.engines.ChaCha7539Engine();
        this.b = mac;
    }
}
