package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class G3413CBCBlockCipher implements org.bouncycastle.crypto.BlockCipher {
    private int a;
    private int b;
    private byte[] c;
    private byte[] d;
    private org.bouncycastle.crypto.BlockCipher e;
    private boolean f = false;
    private boolean g;

    public G3413CBCBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this.b = blockCipher.b();
        this.e = blockCipher;
    }

    private int e(byte[] bArr, int i, byte[] bArr2, int i2) {
        byte[] bArrB = org.bouncycastle.crypto.modes.GOST3413CipherUtil.b(this.c, this.b);
        byte[] bArrC = org.bouncycastle.crypto.modes.GOST3413CipherUtil.c(bArr, this.b, i);
        byte[] bArr3 = new byte[bArrC.length];
        this.e.c(bArrC, 0, bArr3, 0);
        byte[] bArrD = org.bouncycastle.crypto.modes.GOST3413CipherUtil.d(bArr3, bArrB);
        java.lang.System.arraycopy(bArrD, 0, bArr2, i2, bArrD.length);
        if (bArr2.length > i2 + bArrD.length) {
            g(bArrC);
        }
        return bArrD.length;
    }

    private int f(byte[] bArr, int i, byte[] bArr2, int i2) {
        byte[] bArrD = org.bouncycastle.crypto.modes.GOST3413CipherUtil.d(org.bouncycastle.crypto.modes.GOST3413CipherUtil.c(bArr, this.b, i), org.bouncycastle.crypto.modes.GOST3413CipherUtil.b(this.c, this.b));
        int length = bArrD.length;
        byte[] bArr3 = new byte[length];
        this.e.c(bArrD, 0, bArr3, 0);
        java.lang.System.arraycopy(bArr3, 0, bArr2, i2, length);
        if (bArr2.length > i2 + bArrD.length) {
            g(bArr3);
        }
        return length;
    }

    private void g(byte[] bArr) {
        byte[] bArrA = org.bouncycastle.crypto.modes.GOST3413CipherUtil.a(this.c, this.a - this.b);
        java.lang.System.arraycopy(bArrA, 0, this.c, 0, bArrA.length);
        java.lang.System.arraycopy(bArr, 0, this.c, bArrA.length, this.a - bArrA.length);
    }

    private void h() {
        int i = this.a;
        this.c = new byte[i];
        this.d = new byte[i];
    }

    private void i() {
        this.a = this.b;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int b() {
        return this.b;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public int c(byte[] bArr, int i, byte[] bArr2, int i2) {
        return this.g ? f(bArr, i, bArr2, i2) : e(bArr, i, bArr2, i2);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public java.lang.String getAlgorithmName() {
        return this.e.getAlgorithmName() + "/CBC";
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.BlockCipher blockCipher;
        this.g = 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.b) {
                throw new java.lang.IllegalArgumentException("Parameter m must blockSize <= m");
            }
            this.a = bArrA.length;
            h();
            byte[] bArrJ = org.bouncycastle.util.Arrays.j(bArrA);
            this.d = bArrJ;
            java.lang.System.arraycopy(bArrJ, 0, this.c, 0, bArrJ.length);
            if (parametersWithIV.b() != null) {
                blockCipher = this.e;
                cipherParameters = parametersWithIV.b();
                blockCipher.init(z, cipherParameters);
            }
        } else {
            i();
            h();
            byte[] bArr = this.d;
            java.lang.System.arraycopy(bArr, 0, this.c, 0, bArr.length);
            if (cipherParameters != null) {
                blockCipher = this.e;
                blockCipher.init(z, cipherParameters);
            }
        }
        this.f = true;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        if (this.f) {
            byte[] bArr = this.d;
            java.lang.System.arraycopy(bArr, 0, this.c, 0, bArr.length);
            this.e.reset();
        }
    }
}
