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