package org.bouncycastle.crypto.modes;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class G3413CFBBlockCipher extends org.bouncycastle.crypto.StreamBlockCipher {
    private final int b;
    private int c;
    private int d;
    private byte[] e;
    private byte[] f;
    private org.bouncycastle.crypto.BlockCipher g;
    private boolean h;
    private boolean i;
    private byte[] j;
    private byte[] k;
    private int l;

    public G3413CFBBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher) {
        this(blockCipher, blockCipher.b() * 8);
    }

    private void i() {
        int i = this.c;
        this.e = new byte[i];
        this.f = new byte[i];
    }

    private void j() {
        this.c = this.d * 2;
    }

    @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) {
        processBytes(bArr, i, b(), bArr2, i2);
        return b();
    }

    @Override // org.bouncycastle.crypto.StreamBlockCipher
    protected byte f(byte b) {
        if (this.l == 0) {
            this.j = g();
        }
        byte[] bArr = this.j;
        int i = this.l;
        byte b2 = (byte) (bArr[i] ^ b);
        byte[] bArr2 = this.k;
        int i2 = i + 1;
        this.l = i2;
        if (this.h) {
            b = b2;
        }
        bArr2[i] = b;
        if (i2 == b()) {
            this.l = 0;
            h(this.k);
        }
        return b2;
    }

    byte[] g() {
        byte[] bArrB = org.bouncycastle.crypto.modes.GOST3413CipherUtil.b(this.e, this.d);
        byte[] bArr = new byte[bArrB.length];
        this.g.c(bArrB, 0, bArr, 0);
        return org.bouncycastle.crypto.modes.GOST3413CipherUtil.b(bArr, this.b);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public java.lang.String getAlgorithmName() {
        return this.g.getAlgorithmName() + "/CFB" + (this.d * 8);
    }

    void h(byte[] bArr) {
        byte[] bArrA = org.bouncycastle.crypto.modes.GOST3413CipherUtil.a(this.e, this.c - this.b);
        java.lang.System.arraycopy(bArrA, 0, this.e, 0, bArrA.length);
        java.lang.System.arraycopy(bArr, 0, this.e, bArrA.length, this.c - bArrA.length);
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        org.bouncycastle.crypto.BlockCipher blockCipher;
        this.h = 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("Parameter m must blockSize <= m");
            }
            this.c = bArrA.length;
            i();
            byte[] bArrJ = org.bouncycastle.util.Arrays.j(bArrA);
            this.f = bArrJ;
            java.lang.System.arraycopy(bArrJ, 0, this.e, 0, bArrJ.length);
            if (parametersWithIV.b() != null) {
                blockCipher = this.g;
                cipherParameters = parametersWithIV.b();
                blockCipher.init(true, cipherParameters);
            }
        } else {
            j();
            i();
            byte[] bArr = this.f;
            java.lang.System.arraycopy(bArr, 0, this.e, 0, bArr.length);
            if (cipherParameters != null) {
                blockCipher = this.g;
                blockCipher.init(true, cipherParameters);
            }
        }
        this.i = true;
    }

    @Override // org.bouncycastle.crypto.BlockCipher
    public void reset() {
        this.l = 0;
        org.bouncycastle.util.Arrays.i(this.k);
        org.bouncycastle.util.Arrays.i(this.j);
        if (this.i) {
            byte[] bArr = this.f;
            java.lang.System.arraycopy(bArr, 0, this.e, 0, bArr.length);
            this.g.reset();
        }
    }

    public G3413CFBBlockCipher(org.bouncycastle.crypto.BlockCipher blockCipher, int i) {
        super(blockCipher);
        this.i = false;
        if (i < 0 || i > blockCipher.b() * 8) {
            throw new java.lang.IllegalArgumentException("Parameter bitBlockSize must be in range 0 < bitBlockSize <= " + (blockCipher.b() * 8));
        }
        this.d = blockCipher.b();
        this.g = blockCipher;
        this.b = i / 8;
        this.k = new byte[b()];
    }
}
