package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class CCMBlockCipher implements org.bouncycastle.crypto.modes.CCMModeCipher {
    private org.bouncycastle.crypto.BlockCipher a;
    private int b;
    private boolean c;
    private byte[] d;
    private byte[] e;
    private int f;
    private org.bouncycastle.crypto.CipherParameters g;
    private byte[] h;
    private org.bouncycastle.crypto.modes.CCMBlockCipher.ExposedByteArrayOutputStream i = new org.bouncycastle.crypto.modes.CCMBlockCipher.ExposedByteArrayOutputStream();
    private org.bouncycastle.crypto.modes.CCMBlockCipher.ExposedByteArrayOutputStream j = new org.bouncycastle.crypto.modes.CCMBlockCipher.ExposedByteArrayOutputStream();

    private static class ExposedByteArrayOutputStream extends java.io.ByteArrayOutputStream {
        public byte[] a() {
            return ((java.io.ByteArrayOutputStream) this).buf;
        }
    }

    public CCMBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this.a = blockCipher;
        int iB = blockCipher.b();
        this.b = iB;
        this.h = new byte[iB];
        if (iB != 16) {
            throw new java.lang.IllegalArgumentException("cipher required with a block size of 16.");
        }
    }

    private int a(byte[] bArr, int i, int i2, byte[] bArr2) {
        org.bouncycastle.crypto.macs.CBCBlockCipherMac cBCBlockCipherMac = new org.bouncycastle.crypto.macs.CBCBlockCipherMac(this.a, this.f * 8);
        cBCBlockCipherMac.init(this.g);
        byte[] bArr3 = new byte[16];
        if (g()) {
            bArr3[0] = (byte) (bArr3[0] | 64);
        }
        int i3 = 2;
        byte macSize = (byte) (bArr3[0] | ((((cBCBlockCipherMac.getMacSize() - 2) / 2) & 7) << 3));
        bArr3[0] = macSize;
        byte[] bArr4 = this.d;
        bArr3[0] = (byte) (macSize | ((14 - bArr4.length) & 7));
        java.lang.System.arraycopy(bArr4, 0, bArr3, 1, bArr4.length);
        int i4 = i2;
        int i5 = 1;
        while (i4 > 0) {
            bArr3[16 - i5] = (byte) (i4 & 255);
            i4 >>>= 8;
            i5++;
        }
        cBCBlockCipherMac.update(bArr3, 0, 16);
        if (g()) {
            int iE = e();
            if (iE < 65280) {
                cBCBlockCipherMac.update((byte) (iE >> 8));
                cBCBlockCipherMac.update((byte) iE);
            } else {
                cBCBlockCipherMac.update((byte) -1);
                cBCBlockCipherMac.update((byte) -2);
                cBCBlockCipherMac.update((byte) (iE >> 24));
                cBCBlockCipherMac.update((byte) (iE >> 16));
                cBCBlockCipherMac.update((byte) (iE >> 8));
                cBCBlockCipherMac.update((byte) iE);
                i3 = 6;
            }
            byte[] bArr5 = this.e;
            if (bArr5 != null) {
                cBCBlockCipherMac.update(bArr5, 0, bArr5.length);
            }
            if (this.i.size() > 0) {
                cBCBlockCipherMac.update(this.i.a(), 0, this.i.size());
            }
            int i6 = (i3 + iE) % 16;
            if (i6 != 0) {
                while (i6 != 16) {
                    cBCBlockCipherMac.update((byte) 0);
                    i6++;
                }
            }
        }
        cBCBlockCipherMac.update(bArr, i, i2);
        return cBCBlockCipherMac.doFinal(bArr2, 0);
    }

    private int e() {
        int size = this.i.size();
        byte[] bArr = this.e;
        return size + (bArr == null ? 0 : bArr.length);
    }

    private int f(boolean z, int i) {
        if (!z || (i >= 32 && i <= 128 && (i & 15) == 0)) {
            return i >>> 3;
        }
        throw new java.lang.IllegalArgumentException("tag length in octets must be one of {4,6,8,10,12,14,16}");
    }

    private boolean g() {
        return e() > 0;
    }

    public static org.bouncycastle.crypto.modes.CCMModeCipher h(org.bouncycastle.crypto.BlockCipher blockCipher) {
        return new org.bouncycastle.crypto.modes.CCMBlockCipher(blockCipher);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public byte[] b() {
        int i = this.f;
        byte[] bArr = new byte[i];
        java.lang.System.arraycopy(this.h, 0, bArr, 0, i);
        return bArr;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void c(byte[] bArr, int i, int i2) throws java.io.IOException {
        this.i.write(bArr, i, i2);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void d(byte b) throws java.io.IOException {
        this.i.write(b);
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int doFinal(byte[] bArr, int i) throws org.bouncycastle.crypto.InvalidCipherTextException {
        int i2 = i(this.j.a(), 0, this.j.size(), bArr, i);
        reset();
        return i2;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public java.lang.String getAlgorithmName() {
        return this.a.getAlgorithmName() + "/CCM";
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int getOutputSize(int i) {
        int size = i + this.j.size();
        boolean z = this.c;
        int i2 = this.f;
        if (z) {
            return size + i2;
        }
        if (size < i2) {
            return 0;
        }
        return size - i2;
    }

    @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;
    }

    public int i(byte[] bArr, int i, int i2, byte[] bArr2, int i3) throws org.bouncycastle.crypto.InvalidCipherTextException {
        int i4;
        if (this.g == null) {
            throw new java.lang.IllegalStateException("CCM cipher unitialized.");
        }
        byte[] bArr3 = this.d;
        int length = bArr3.length;
        int i5 = 15 - length;
        if (i5 < 4) {
            if (i2 - (!this.c ? 16 : 0) >= (1 << (i5 * 8))) {
                throw new java.lang.IllegalStateException("CCM packet too large for choice of q");
            }
        }
        byte[] bArr4 = new byte[this.b];
        bArr4[0] = (byte) ((14 - length) & 7);
        java.lang.System.arraycopy(bArr3, 0, bArr4, 1, bArr3.length);
        org.bouncycastle.crypto.modes.CTRModeCipher cTRModeCipherN = org.bouncycastle.crypto.modes.SICBlockCipher.n(this.a);
        cTRModeCipherN.init(this.c, new org.bouncycastle.crypto.params.ParametersWithIV(this.g, bArr4));
        if (!this.c) {
            int i6 = this.f;
            if (i2 < i6) {
                throw new org.bouncycastle.crypto.InvalidCipherTextException("data too short");
            }
            int i7 = i2 - i6;
            if (bArr2.length < i7 + i3) {
                throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short.");
            }
            int i8 = i + i7;
            java.lang.System.arraycopy(bArr, i8, this.h, 0, i6);
            byte[] bArr5 = this.h;
            cTRModeCipherN.c(bArr5, 0, bArr5, 0);
            int i9 = this.f;
            while (true) {
                byte[] bArr6 = this.h;
                if (i9 == bArr6.length) {
                    break;
                }
                bArr6[i9] = 0;
                i9++;
            }
            int i10 = i;
            int i11 = i3;
            while (true) {
                i4 = this.b;
                if (i10 >= i8 - i4) {
                    break;
                }
                cTRModeCipherN.c(bArr, i10, bArr2, i11);
                int i12 = this.b;
                i11 += i12;
                i10 += i12;
            }
            byte[] bArr7 = new byte[i4];
            int i13 = i7 - (i10 - i);
            java.lang.System.arraycopy(bArr, i10, bArr7, 0, i13);
            cTRModeCipherN.c(bArr7, 0, bArr7, 0);
            java.lang.System.arraycopy(bArr7, 0, bArr2, i11, i13);
            byte[] bArr8 = new byte[this.b];
            a(bArr2, i3, i7, bArr8);
            if (org.bouncycastle.util.Arrays.z(this.h, bArr8)) {
                return i7;
            }
            throw new org.bouncycastle.crypto.InvalidCipherTextException("mac check in CCM failed");
        }
        int i14 = this.f + i2;
        if (bArr2.length < i14 + i3) {
            throw new org.bouncycastle.crypto.OutputLengthException("Output buffer too short.");
        }
        a(bArr, i, i2, this.h);
        byte[] bArr9 = new byte[this.b];
        cTRModeCipherN.c(this.h, 0, bArr9, 0);
        int i15 = i;
        int i16 = i3;
        while (true) {
            int i17 = i + i2;
            int i18 = this.b;
            if (i15 >= i17 - i18) {
                byte[] bArr10 = new byte[i18];
                int i19 = i17 - i15;
                java.lang.System.arraycopy(bArr, i15, bArr10, 0, i19);
                cTRModeCipherN.c(bArr10, 0, bArr10, 0);
                java.lang.System.arraycopy(bArr10, 0, bArr2, i16, i19);
                java.lang.System.arraycopy(bArr9, 0, bArr2, i3 + i2, this.f);
                return i14;
            }
            cTRModeCipherN.c(bArr, i15, bArr2, i16);
            int i20 = this.b;
            i16 += i20;
            i15 += i20;
        }
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.CipherParameters cipherParametersB;
        this.c = z;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.AEADParameters) {
            org.bouncycastle.crypto.params.AEADParameters aEADParameters = (org.bouncycastle.crypto.params.AEADParameters) cipherParameters;
            this.d = aEADParameters.d();
            this.e = aEADParameters.a();
            this.f = f(z, aEADParameters.c());
            cipherParametersB = aEADParameters.b();
        } else {
            if (!(cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV)) {
                throw new java.lang.IllegalArgumentException("invalid parameters passed to CCM: " + cipherParameters.getClass().getName());
            }
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            this.d = parametersWithIV.a();
            this.e = null;
            this.f = f(z, 64);
            cipherParametersB = parametersWithIV.b();
        }
        if (cipherParametersB != null) {
            this.g = cipherParametersB;
        }
        byte[] bArr = this.d;
        if (bArr == null || bArr.length < 7 || bArr.length > 13) {
            throw new java.lang.IllegalArgumentException("nonce must have length from 7 to 13 octets");
        }
        reset();
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public int processByte(byte b, byte[] bArr, int i) throws java.io.IOException {
        this.j.write(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 {
        if (bArr.length < i + i2) {
            throw new org.bouncycastle.crypto.DataLengthException("Input buffer too short");
        }
        this.j.write(bArr, i, i2);
        return 0;
    }

    @Override // org.bouncycastle.crypto.modes.AEADCipher
    public void reset() {
        this.a.reset();
        this.i.reset();
        this.j.reset();
    }
}
