package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class CBCBlockCipher extends org.bouncycastle.crypto.DefaultMultiBlockCipher implements org.bouncycastle.crypto.modes.CBCModeCipher {
    private byte[] a;
    private byte[] b;
    private byte[] c;
    private int d;
    private org.bouncycastle.crypto.BlockCipher e;
    private boolean f;

    public CBCBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this.e = blockCipher;
        int iB = blockCipher.b();
        this.d = iB;
        this.a = new byte[iB];
        this.b = new byte[iB];
        this.c = new byte[iB];
    }

    private int f(byte[] bArr, int i, byte[] bArr2, int i2) {
        int i3 = this.d;
        if (i + i3 > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        java.lang.System.arraycopy(bArr, i, this.c, 0, i3);
        int iC = this.e.c(bArr, i, bArr2, i2);
        for (int i4 = 0; i4 < this.d; i4++) {
            int i5 = i2 + i4;
            bArr2[i5] = (byte) (bArr2[i5] ^ this.b[i4]);
        }
        byte[] bArr3 = this.b;
        this.b = this.c;
        this.c = bArr3;
        return iC;
    }

    private int g(byte[] bArr, int i, byte[] bArr2, int i2) {
        if (this.d + i > bArr.length) {
            throw new org.bouncycastle.crypto.DataLengthException("input buffer too short");
        }
        for (int i3 = 0; i3 < this.d; i3++) {
            byte[] bArr3 = this.b;
            bArr3[i3] = (byte) (bArr3[i3] ^ bArr[i + i3]);
        }
        int iC = this.e.c(this.b, 0, bArr2, i2);
        byte[] bArr4 = this.b;
        java.lang.System.arraycopy(bArr2, i2, bArr4, 0, bArr4.length);
        return iC;
    }

    public static org.bouncycastle.crypto.modes.CBCModeCipher h(org.bouncycastle.crypto.BlockCipher blockCipher) {
        return new org.bouncycastle.crypto.modes.CBCBlockCipher(blockCipher);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int b() {
        return this.e.b();
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int c(byte[] bArr, int i, byte[] bArr2, int i2) {
        return this.f ? g(bArr, i, bArr2, i2) : f(bArr, i, bArr2, i2);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public java.lang.String getAlgorithmName() {
        return this.e.getAlgorithmName() + "/CBC";
    }

    @Override // org.bouncycastle.crypto.modes.CBCModeCipher
    public org.bouncycastle.crypto.BlockCipher getUnderlyingCipher() {
        return this.e;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        boolean z2 = this.f;
        this.f = z;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithIV) {
            org.bouncycastle.crypto.params.ParametersWithIV parametersWithIV = (org.bouncycastle.crypto.params.ParametersWithIV) cipherParameters;
            byte[] bArrA = parametersWithIV.a();
            if (bArrA.length != this.d) {
                throw new java.lang.IllegalArgumentException("initialisation vector must be the same length as block size");
            }
            java.lang.System.arraycopy(bArrA, 0, this.a, 0, bArrA.length);
            cipherParameters = parametersWithIV.b();
        } else {
            org.bouncycastle.util.Arrays.G(this.a, (byte) 0);
        }
        reset();
        if (cipherParameters != null) {
            this.e.init(z, cipherParameters);
        } else if (z2 != z) {
            throw new java.lang.IllegalArgumentException("cannot change encrypting state without providing key.");
        }
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        byte[] bArr = this.a;
        java.lang.System.arraycopy(bArr, 0, this.b, 0, bArr.length);
        org.bouncycastle.util.Arrays.G(this.c, (byte) 0);
        this.e.reset();
    }
}
